Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlizarraga committed Oct 12, 2023
1 parent 429fea9 commit 1567536
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,29 @@ static Status GetInitializerInputData(const NodeUnitIODef& input, const QnnModel
OnnxInputInfo input_info = {};
ORT_RETURN_IF_ERROR(qnn_model_wrapper.GetOnnxInputInfo(input, input_info));
ORT_RETURN_IF_NOT(input_info.is_initializer,
"QNN requires the starts, ends, axes, and steps inputs to "
"be initializers");
"QNN requires the starts, ends, axes, and steps inputs to be initializers");

std::vector<uint8_t> initializer_bytes;

// Note: UnpackInitializerData() uses ORT's protobuf utilities, which ensure that the initializer bytes are
// contiguous, aligned, and in the appropriate endianness. This is necessary to be able to reinterpret bytes
// as an array of larger elements.
ORT_RETURN_IF_ERROR(qnn_model_wrapper.UnpackInitializerData(*input_info.initializer_tensor, initializer_bytes));

size_t tensor_byte_size = initializer_bytes.size();
const auto data_type = input_info.initializer_tensor->data_type();

Status status;

switch (data_type) {
case ONNX_NAMESPACE::TensorProto_DataType_INT64: {
const int64_t* tensor_data = reinterpret_cast<const int64_t*>(initializer_bytes.data());
size_t size = tensor_byte_size / sizeof(int64_t);
output.insert(output.end(), tensor_data, tensor_data + size);
gsl::span<const int64_t> elements = qnn::utils::ReinterpretBytesAsSpan<int64_t>(initializer_bytes.data(),
initializer_bytes.size());
output.insert(output.end(), elements.begin(), elements.end());
break;
}
case ONNX_NAMESPACE::TensorProto_DataType_INT32: {
const int32_t* tensor_data = reinterpret_cast<const int32_t*>(initializer_bytes.data());
size_t size = tensor_byte_size / sizeof(int32_t);
output.insert(output.end(), tensor_data, tensor_data + size);
gsl::span<const int32_t> elements = qnn::utils::ReinterpretBytesAsSpan<int32_t>(initializer_bytes.data(),
initializer_bytes.size());
output.insert(output.end(), elements.begin(), elements.end());
break;
}
default:
Expand Down Expand Up @@ -174,9 +175,12 @@ Status SliceOpBuilder::ProcessAttributesAndOutputs(QnnModelWrapper& qnn_model_wr
onnxruntime::SliceOp::PrepareForComputeMetadata compute_metadata(input_dimensions);
ORT_RETURN_IF_ERROR(SliceOp::PrepareForComputeHelper(raw_starts, raw_ends, raw_axes, raw_steps, compute_metadata));

std::vector<uint32_t> ranges_dims{static_cast<uint32_t>(input_dimensions.size()), 3};
const size_t input_rank = input_dimensions.size();
std::vector<uint32_t> ranges_dims{static_cast<uint32_t>(input_rank), 3};
std::vector<uint32_t> ranges_data;
for (size_t i = 0; i < input_dimensions.size(); i++) {
ranges_data.reserve(input_rank);

for (size_t i = 0; i < input_rank; i++) {
ranges_data.push_back(static_cast<uint32_t>(compute_metadata.starts_[i]));
ranges_data.push_back(static_cast<uint32_t>(compute_metadata.ends_[i]));
ranges_data.push_back(static_cast<uint32_t>(compute_metadata.steps_[i]));
Expand Down
8 changes: 8 additions & 0 deletions onnxruntime/core/providers/qnn/builder/qnn_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
#include <numeric>
#include <vector>
#include <string>
#include <gsl/span>

Check warning on line 11 in onnxruntime/core/providers/qnn/builder/qnn_utils.h

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] onnxruntime/core/providers/qnn/builder/qnn_utils.h#L11

Found C system header after other header. Should be: qnn_utils.h, c system, c++ system, other. [build/include_order] [4]
Raw output
onnxruntime/core/providers/qnn/builder/qnn_utils.h:11:  Found C system header after other header. Should be: qnn_utils.h, c system, c++ system, other.  [build/include_order] [4]

namespace onnxruntime {
namespace qnn {
class QnnOpConfigWrapper;

namespace utils {

// Reinterprets an array of contiguous bytes in the target's endianness to a span of elements.
template <typename T>
inline gsl::span<const T> ReinterpretBytesAsSpan(const uint8_t* data, size_t num_bytes) {
return gsl::span<const T>(reinterpret_cast<const T*>(data), num_bytes / sizeof(T));
}

size_t GetElementSizeByType(const Qnn_DataType_t& data_type);

size_t GetElementSizeByType(ONNXTensorElementDataType elem_type);
Expand Down

0 comments on commit 1567536

Please sign in to comment.