Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions js/web/lib/wasm/jsep/webgpu/ops/quantize-linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const validateInputs = (inputs: readonly TensorView[], attributes: DequantizeLin
if (inputs.length === 3 && inputs[0].dataType !== inputs[2].dataType) {
throw new Error('x and x-zero-point must have the same data type.');
}
if (inputs[0].dataType === DataType.int32 && inputs.length > 2) {
throw new Error('In the case of dequantizing int32 there is no zero point.');
}
if (inputs[1].dims.length !== 0 && inputs[1].dims.length !== 1 && inputs[1].dims.length !== inputs[0].dims.length) {
throw new Error('scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.');
}
Expand Down
183 changes: 173 additions & 10 deletions js/web/test/data/ops/dequantizelinear.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"attributes": [],
"cases": [
{
"name": "T[1]",
"name": "uint8 per-tensor with zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
Expand Down Expand Up @@ -41,7 +41,7 @@
"attributes": [],
"cases": [
{
"name": "T[2]",
"name": "int32 per-tensor no zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
Expand All @@ -64,6 +64,41 @@
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
"opset": { "domain": "", "version": 10 },
"attributes": [],
"cases": [
{
"name": "int32 per-tensor with zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
"dims": [4],
"type": "int32"
},
{
"data": [0.1],
"dims": [1],
"type": "float32"
},
{
"data": [1],
"dims": [1],
"type": "int32"
}
],
"outputs": [
{
"data": [0.0, 0.1, 0.2, 0.3],
"dims": [4],
"type": "float32"
}
]
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
Expand All @@ -77,7 +112,7 @@
],
"cases": [
{
"name": "T[3]",
"name": "uint8 2D per-axis scalar scale with zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
Expand Down Expand Up @@ -118,7 +153,7 @@
],
"cases": [
{
"name": "T[4]",
"name": "int32 2D per-axis scalar scale no zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
Expand Down Expand Up @@ -154,7 +189,48 @@
],
"cases": [
{
"name": "T[5]",
"name": "int32 2D per-axis scalar scale with zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
"dims": [2, 2],
"type": "int32"
},
{
"data": [0.1],
"dims": [1],
"type": "float32"
},
{
"data": [1],
"dims": [1],
"type": "int32"
}
],
"outputs": [
{
"data": [0.0, 0.1, 0.2, 0.3],
"dims": [2, 2],
"type": "float32"
}
]
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
"opset": { "domain": "", "version": 13 },
"attributes": [
{
"name": "axis",
"data": 1,
"type": "int"
}
],
"cases": [
{
"name": "uint8 3D per-axis uniform scale with zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
Expand Down Expand Up @@ -195,7 +271,7 @@
],
"cases": [
{
"name": "T[6]",
"name": "uint8 3D per-axis varying scale with zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
Expand Down Expand Up @@ -236,7 +312,7 @@
],
"cases": [
{
"name": "T[7]",
"name": "int32 3D per-axis scalar scale no zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
Expand All @@ -259,6 +335,47 @@
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
"opset": { "domain": "", "version": 13 },
"attributes": [
{
"name": "axis",
"data": 1,
"type": "int"
}
],
"cases": [
{
"name": "int32 3D per-axis scalar scale with zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
"dims": [2, 2, 2],
"type": "int32"
},
{
"data": [0.1],
"dims": [1],
"type": "float32"
},
{
"data": [1],
"dims": [1],
"type": "int32"
}
],
"outputs": [
{
"data": [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7],
"dims": [2, 2, 2],
"type": "float32"
}
]
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
Expand All @@ -277,7 +394,7 @@
],
"cases": [
{
"name": "T[8]",
"name": "uint8 3D blocked with zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
Expand Down Expand Up @@ -323,7 +440,7 @@
],
"cases": [
{
"name": "T[9]",
"name": "int32 3D blocked no zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
Expand All @@ -346,6 +463,52 @@
}
]
},
{
"name": "dequantizelinear block dequantization",
"operator": "DequantizeLinear",
"opset": { "domain": "", "version": 21 },
"attributes": [
{
"name": "axis",
"data": 1,
"type": "int"
},
{
"name": "block_size",
"data": 2,
"type": "int"
}
],
"cases": [
{
"name": "int32 3D blocked with zero point",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
"dims": [2, 2, 2],
"type": "int32"
},
{
"data": [0.1, 0.2, 0.3, 0.4],
"dims": [2, 1, 2],
"type": "float32"
},
{
"data": [0, 1, 0, 1],
"dims": [2, 1, 2],
"type": "int32"
}
],
"outputs": [
{
"data": [0.1, 0.2, 0.3, 0.6, 1.5, 2.0, 2.1, 2.8],
"dims": [2, 2, 2],
"type": "float32"
}
]
}
]
},
{
"name": "dequantizelinear",
"operator": "DequantizeLinear",
Expand All @@ -359,7 +522,7 @@
],
"cases": [
{
"name": "T[3]",
"name": "uint8 2D per-axis scalar scale no zero point",
"inputs": [
{
"data": [1, 2, 3, 4],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Status DequantizeLinearProgram::GenerateShaderCode(ShaderHelper& shader) const {
if (packed_) {
shader.MainFunctionBody()
<< "let zero_point_index = " << output.IndicesGet("output_indices", "uniforms.axis") << ";\n"
<< "let zero_point_input = " << zero_point.GetByOffset("u32(zero_point_index / 4)") << ";\n"
<< "let zero_point_input = " << zero_point.GetByOffset("zero_point_index / 4") << ";\n"
<< "let zero_point_vec = " << unpack << ";\n"
<< "let zero_point_value = zero_point_vec[zero_point_index % 4];\n";
} else {
Expand All @@ -88,16 +88,17 @@ Status DequantizeLinearProgram::GenerateShaderCode(ShaderHelper& shader) const {
<< "let zero_point_value = " << zero_point.GetByOffset("zero_point_index") << ";\n";
}
} else {
// BlockedQuantization. The zero-point input shape is same as the input shape except along axis.
// BlockedQuantization. The zero-point input shape is the same as the scale input shape.
if (packed_) {
shader.MainFunctionBody()
<< "let zero_point_offset = " << scale.GetByIndices("scale_indices") << ";\n"
<< "let zero_point_input = " << zero_point.GetByOffset("u32(zero_point_offset / 4)") << ";\n"
<< "let zero_point_offset = " << scale.IndicesToOffset("scale_indices") << ";\n"
<< "let zero_point_input = " << zero_point.GetByOffset("zero_point_offset / 4") << ";\n"
<< "let zero_point_vec = " << unpack << ";\n"
<< "let zero_point_value = zero_point_vec[zero_point_offset % 4];\n";
Comment thread
edgchen1 marked this conversation as resolved.
} else {
shader.MainFunctionBody()
<< "let zero_point_value = " << zero_point.GetByIndices("scale_indices") << ";\n";
<< "let zero_point_offset = " << scale.IndicesToOffset("scale_indices") << ";\n"
<< "let zero_point_value = " << zero_point.GetByOffset("zero_point_offset") << ";\n";
}
}
} else {
Expand Down Expand Up @@ -145,7 +146,9 @@ Status DequantizeLinear::ComputeInternal(ComputeContext& context) const {
program
.AddInputs({{x, ProgramTensorMetadataDependency::TypeAndRank, ProgramInput::Flatten, packed ? 4 : input_component}})
.AddInputs({{x_scale, ProgramTensorMetadataDependency::TypeAndRank}})
.AddOutput({output_tensor, ProgramTensorMetadataDependency::Rank, components})
.AddOutput(use_components
? ProgramOutput{output_tensor, ProgramTensorMetadataDependency::Rank, ProgramOutput::Flatten, components}
: ProgramOutput{output_tensor, ProgramTensorMetadataDependency::Rank, components})
.SetDispatchGroupSize((x_size / components + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE)
Comment thread
edgchen1 marked this conversation as resolved.
.AddUniformVariables({{static_cast<uint32_t>(axis)}})
.AddUniformVariables({{static_cast<uint32_t>(block_size_)}})
Expand Down
Loading