-
Notifications
You must be signed in to change notification settings - Fork 619
[ET-VK][Ops] quantize ops skeleton test framework #11366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3c822c7
[ET-VK][Ops] quantize ops skeleton test framework
95517b0
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
c043984
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
185edc3
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
56c38dc
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
be1b485
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
7dd5285
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
0e794fd
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
fdcb3f5
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
f02d543
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
dc9424d
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
50b246f
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
33c7719
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
e5894a9
Update on "[ET-VK][Ops] quantize ops skeleton test framework"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,276 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <ATen/ATen.h> | ||
|
||
#include <executorch/backends/vulkan/runtime/api/api.h> | ||
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h> | ||
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h> | ||
|
||
#include <executorch/extension/aten_util/make_aten_functor_from_et_functor.h> | ||
#include <executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h> | ||
|
||
#include <cassert> | ||
#include <iostream> | ||
|
||
namespace torch { | ||
namespace executor { | ||
namespace native { | ||
|
||
// Forward declarations of the functions we're testing | ||
Tensor& quantize_per_tensor_out( | ||
const Tensor& input, | ||
double scale, | ||
int64_t zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
ScalarType dtype, | ||
Tensor& out); | ||
|
||
Tensor& quantize_per_token_out( | ||
const Tensor& input, | ||
const Tensor& scale, | ||
const Tensor& zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
ScalarType dtype, | ||
Tensor& out); | ||
|
||
// Wrapper function for quantize_per_tensor_out without context | ||
Tensor& quantize_per_tensor_out_no_context( | ||
const Tensor& input, | ||
double scale, | ||
int64_t zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
ScalarType dtype, | ||
Tensor& out) { | ||
return torch::executor::native::quantize_per_tensor_out( | ||
input, scale, zero_point, quant_min, quant_max, dtype, out); | ||
} | ||
|
||
// Wrapper function for quantize_per_token_out without context | ||
Tensor& quantize_per_token_out_no_context( | ||
const Tensor& input, | ||
const Tensor& scale, | ||
const Tensor& zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
ScalarType dtype, | ||
Tensor& out) { | ||
return torch::executor::native::quantize_per_token_out( | ||
input, scale, zero_point, quant_min, quant_max, dtype, out); | ||
} | ||
|
||
// ATen wrapper for quantize_per_tensor | ||
at::Tensor quantize_per_tensor_aten( | ||
const at::Tensor& input, | ||
double scale, | ||
int64_t zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
at::ScalarType dtype) { | ||
auto out = at::empty_like(input, dtype); | ||
// Convert at::ScalarType to executorch::ScalarType | ||
ScalarType et_dtype; | ||
switch (dtype) { | ||
case at::kByte: | ||
et_dtype = ScalarType::Byte; | ||
break; | ||
case at::kChar: | ||
et_dtype = ScalarType::Char; | ||
break; | ||
case at::kShort: | ||
et_dtype = ScalarType::Short; | ||
break; | ||
case at::kInt: | ||
et_dtype = ScalarType::Int; | ||
break; | ||
case at::kLong: | ||
et_dtype = ScalarType::Long; | ||
break; | ||
case at::kFloat: | ||
et_dtype = ScalarType::Float; | ||
break; | ||
case at::kDouble: | ||
et_dtype = ScalarType::Double; | ||
break; | ||
default: | ||
throw std::runtime_error("Unsupported dtype"); | ||
} | ||
|
||
WRAP_TO_ATEN(quantize_per_tensor_out_no_context, 6) | ||
(input, scale, zero_point, quant_min, quant_max, et_dtype, out); | ||
return out; | ||
} | ||
|
||
// ATen wrapper for quantize_per_token | ||
at::Tensor quantize_per_token_aten( | ||
const at::Tensor& input, | ||
const at::Tensor& scale, | ||
const at::Tensor& zero_point, | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
at::ScalarType dtype) { | ||
auto out = at::empty_like(input, dtype); | ||
// Convert at::ScalarType to executorch::ScalarType | ||
ScalarType et_dtype; | ||
switch (dtype) { | ||
case at::kByte: | ||
et_dtype = ScalarType::Byte; | ||
break; | ||
case at::kChar: | ||
et_dtype = ScalarType::Char; | ||
break; | ||
case at::kShort: | ||
et_dtype = ScalarType::Short; | ||
break; | ||
case at::kInt: | ||
et_dtype = ScalarType::Int; | ||
break; | ||
case at::kLong: | ||
et_dtype = ScalarType::Long; | ||
break; | ||
case at::kFloat: | ||
et_dtype = ScalarType::Float; | ||
break; | ||
case at::kDouble: | ||
et_dtype = ScalarType::Double; | ||
break; | ||
default: | ||
throw std::runtime_error("Unsupported dtype"); | ||
} | ||
|
||
WRAP_TO_ATEN(quantize_per_token_out_no_context, 6) | ||
(input, scale, zero_point, quant_min, quant_max, et_dtype, out); | ||
return out; | ||
} | ||
|
||
} // namespace native | ||
} // namespace executor | ||
} // namespace torch | ||
|
||
// | ||
// Test functions | ||
// | ||
|
||
// Helper function to get the name of a ScalarType for better error messages | ||
std::string scalar_type_name(c10::ScalarType dtype) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comments on #11480 |
||
switch (dtype) { | ||
case c10::kLong: | ||
return "c10::kLong"; | ||
case c10::kShort: | ||
return "c10::kShort"; | ||
case c10::kComplexHalf: | ||
return "c10::kComplexHalf"; | ||
case c10::kComplexFloat: | ||
return "c10::kComplexFloat"; | ||
case c10::kComplexDouble: | ||
return "c10::kComplexDouble"; | ||
case c10::kBool: | ||
return "c10::kBool"; | ||
case c10::kQInt8: | ||
return "c10::kQInt8"; | ||
case c10::kQUInt8: | ||
return "c10::kQUInt8"; | ||
case c10::kQInt32: | ||
return "c10::kQInt32"; | ||
case c10::kBFloat16: | ||
return "c10::kBFloat16"; | ||
case c10::kQUInt4x2: | ||
return "c10::kQUInt4x2"; | ||
case c10::kQUInt2x4: | ||
return "c10::kQUInt2x4"; | ||
default: | ||
return "Unknown(" + std::to_string(static_cast<int>(dtype)) + ")"; | ||
} | ||
} | ||
|
||
vkcompute::vkapi::ScalarType from_at_scalartype(c10::ScalarType at_scalartype) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comments on #11480 |
||
using namespace vkcompute; | ||
switch (at_scalartype) { | ||
case c10::kFloat: | ||
return vkapi::kFloat; | ||
case c10::kHalf: | ||
return vkapi::kHalf; | ||
case c10::kInt: | ||
return vkapi::kInt; | ||
case c10::kLong: | ||
// We don't have inherent vkapi::kLong, use kInt instead | ||
return vkapi::kInt; | ||
case c10::kChar: | ||
return vkapi::kChar; | ||
case c10::kByte: | ||
return vkapi::kByte; | ||
case c10::kDouble: | ||
return vkapi::kDouble; | ||
case c10::kShort: | ||
return vkapi::kShort; | ||
case c10::kUInt16: | ||
return vkapi::kUInt16; | ||
default: | ||
VK_THROW( | ||
"Unsupported at::ScalarType: ", | ||
scalar_type_name(at_scalartype), | ||
" (", | ||
static_cast<int>(at_scalartype), | ||
")"); | ||
} | ||
} | ||
|
||
void check_quantize_args( | ||
int64_t quant_min, | ||
int64_t quant_max, | ||
c10::ScalarType out_dtype) { | ||
using namespace vkcompute; | ||
int32_t quant_min_lower_bound = 0, quant_max_upper_bound = 0; | ||
switch (out_dtype) { | ||
case c10::kByte: | ||
quant_min_lower_bound = | ||
static_cast<int32_t>(std::numeric_limits<uint8_t>::min()); | ||
quant_max_upper_bound = | ||
static_cast<int32_t>(std::numeric_limits<uint8_t>::max()); | ||
break; | ||
case c10::kChar: | ||
quant_min_lower_bound = | ||
static_cast<int32_t>(std::numeric_limits<int8_t>::min()); | ||
quant_max_upper_bound = | ||
static_cast<int32_t>(std::numeric_limits<int8_t>::max()); | ||
break; | ||
case c10::kBits16: | ||
case c10::kUInt16: | ||
quant_min_lower_bound = std::numeric_limits<uint16_t>::min(); | ||
quant_max_upper_bound = std::numeric_limits<uint16_t>::max(); | ||
break; | ||
case c10::kShort: | ||
quant_min_lower_bound = std::numeric_limits<int16_t>::min(); | ||
quant_max_upper_bound = std::numeric_limits<int16_t>::max(); | ||
break; | ||
case c10::kInt: | ||
quant_min_lower_bound = std::numeric_limits<int32_t>::min(); | ||
quant_max_upper_bound = std::numeric_limits<int32_t>::max(); | ||
break; | ||
default: | ||
VK_CHECK_COND(false, "Unsupported dtype: ", scalar_type_name(out_dtype)); | ||
} | ||
VK_CHECK_COND( | ||
quant_min >= quant_min_lower_bound, | ||
"quant_min out of bound for dtype, expected quant_min_lower_bound: ", | ||
quant_min_lower_bound, | ||
" actual quant_min: ", | ||
quant_min); | ||
|
||
VK_CHECK_COND( | ||
quant_max <= quant_max_upper_bound, | ||
"quant_max out of bound for dtype, expected quant_max_upper_bound: ", | ||
quant_max_upper_bound, | ||
" actual quant_max: ", | ||
quant_max); | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comments on #11480