Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QuantOps] Add the quant region definition
Summary: This regional op in the QuantOps dialect will be used to wrap high-precision ops into atomic units for quantization. All the values used by the internal ops are captured explicitly by the op inputs. The quantization parameters of the inputs and outputs are stored in the attributes. Subscribers: jfb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, Joonsoo, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D75972
- Loading branch information
Showing
4 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s | ||
|
||
// CHECK-LABEL: @source | ||
func @source(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [f32, f32, f32], output_specs = [f32], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// CHECK-LABEL: @annotated | ||
func @annotated(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, f32], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// CHECK-LABEL: @quantized | ||
func @quantized(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, !quant.uniform<i32:f32, 2.0>], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// ----- | ||
|
||
func @unmatched_quantize(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
// @expected-error @+1 {{'quant.region' op has incompatible specification !quant.uniform<i32:f16, 3.000000e+00> and input type 'tensor<4xf32>'}} | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, !quant.uniform<i32:f16, 3.0>], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// ----- | ||
|
||
func @unmatched_primitive(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
// @expected-error @+1 {{'quant.region' op has incompatible specification i32 and input type 'tensor<4xf32>'}} | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, i32], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// ----- | ||
|
||
func @unmatched_number(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
// @expected-error @+1 {{'quant.region' op has unmatched operands/results number and spec attributes number}} | ||
%0 = "quant.region"(%arg0, %arg1, %arg2) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
%14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|
||
// ----- | ||
|
||
func @isolated(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) { | ||
// @expected-note @+1 {{required by region isolation constraints}} | ||
%0 = "quant.region"(%arg0, %arg1) ({ | ||
^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>): | ||
%13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
// @expected-error @+1 {{'bar' op using value defined outside the region}} | ||
%14 = "bar"(%13, %arg2) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> | ||
"quant.return"(%14) : (tensor<4xf32>) -> () | ||
}) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>], | ||
output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"} | ||
: (tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) | ||
return %0 : tensor<4xf32> | ||
} | ||
|