-
Notifications
You must be signed in to change notification settings - Fork 886
Qualcomm AI Engine Direct - Adding QNN backend support for rand core ATen op #18298
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 all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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,79 @@ | ||
| # Copyright (c) Qualcomm Innovation Center, Inc. | ||
| # 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. | ||
| from typing import Dict | ||
|
|
||
| import executorch.backends.qualcomm.python.PyQnnManagerAdaptor as PyQnnManager | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from executorch.backends.qualcomm.utils.constants import QCOM_DATA | ||
|
|
||
| from .node_visitor import NodeVisitor | ||
| from .node_visitor_manager import register_node_visitor | ||
| from .qnn_constants import OpRandomUniformLike, QNN_OP_PACKAGE_NAME_QTI_AISW | ||
|
|
||
|
|
||
| @register_node_visitor | ||
| class Rand(NodeVisitor): | ||
| target = ["aten.rand.default", "aten.rand_like.default"] | ||
|
|
||
| def __init__(self, *args) -> None: | ||
| super().__init__(*args) | ||
|
|
||
| def define_node( | ||
| self, | ||
| node: torch.fx.Node, | ||
| nodes_to_wrappers: Dict[torch.fx.Node, PyQnnManager.TensorWrapper], | ||
| ) -> PyQnnManager.PyQnnOpWrapper: | ||
| output_tensor = node.meta["val"] | ||
| output_shape = list(output_tensor.shape) | ||
|
|
||
| shape_data = np.array(output_shape, dtype=np.uint32) | ||
| shape_dims = [len(output_shape)] | ||
|
|
||
| shape_tensor_wrapper = PyQnnManager.TensorWrapper( | ||
| f"{node.name}_shape", | ||
| PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_STATIC, | ||
| PyQnnManager.Qnn_DataType_t.QNN_DATATYPE_UINT_32, # QNN only supports UINT32 for the RandomUniformLike op input | ||
| PyQnnManager.Qnn_QuantizationEncoding_t.QNN_QUANTIZATION_ENCODING_UNDEFINED, | ||
| {}, | ||
| len(shape_dims), | ||
| shape_dims, | ||
| [], | ||
| shape_data, | ||
| True, | ||
| ) | ||
|
|
||
| output_tensor_wrapper = self.define_tensor( | ||
| node, | ||
| node, | ||
| output_tensor, | ||
| PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, | ||
| nodes_to_wrappers, | ||
| ) | ||
|
|
||
| rand_op = PyQnnManager.PyQnnOpWrapper( | ||
| node.name, | ||
| QNN_OP_PACKAGE_NAME_QTI_AISW, | ||
| OpRandomUniformLike.op_name, | ||
| ) | ||
|
|
||
| rand_op.AddInputTensors([shape_tensor_wrapper]) | ||
| rand_op.AddOutputTensors([output_tensor_wrapper]) | ||
|
|
||
| rand_op.AddScalarParam( | ||
| OpRandomUniformLike.param_low, | ||
| PyQnnManager.Qnn_DataType_t.QNN_DATATYPE_FLOAT_32, | ||
| {QCOM_DATA: np.float32(0.0)}, | ||
| ) | ||
|
|
||
| rand_op.AddScalarParam( | ||
| OpRandomUniformLike.param_high, | ||
| PyQnnManager.Qnn_DataType_t.QNN_DATATYPE_FLOAT_32, | ||
| {QCOM_DATA: np.float32(1.0)}, | ||
| ) | ||
|
|
||
| return rand_op | ||
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
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
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.
Nit: Add input type guard to support only int32?
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.
Valid comment, I'll clarify since the description was inaccurate. This op doesn't use input values, just the input's shape, so technically it can "support" input types other than
UINT32, it just doesn't make a difference. I clarified the description, added a comment in the code and a floating-point test for good measure.