Skip to content

prepack serialization follow up PR - #22670

Closed
frank-dong-ms wants to merge 20 commits into
mainfrom
frdong/prepack_2
Closed

prepack serialization follow up PR#22670
frank-dong-ms wants to merge 20 commits into
mainfrom
frdong/prepack_2

Conversation

@frank-dong-ms

Copy link
Copy Markdown
Contributor

Description

follow up PR of : #22256

change rest kernels with prepack implemented

@frank-dong-ms
frank-dong-ms requested a review from a team as a code owner October 31, 2024 06:48
size_t GetMemoryAlignedOffset(size_t current_offset) {
const size_t alignment_number_of_bytes = 64;

return ((current_offset - 1) / alignment_number_of_bytes + 1) * alignment_number_of_bytes;

Check warning

Code scanning / PREfast

You can attempt to make 'onnxruntime::utils::GetMemoryAlignedOffset' constexpr unless it contains any undefined behavior (f.4).

You can attempt to make 'onnxruntime::utils::GetMemoryAlignedOffset' constexpr unless it contains any undefined behavior (f.4).
const size_t alignment_number_of_bytes = 64;

return ((current_offset - 1) / alignment_number_of_bytes + 1) * alignment_number_of_bytes;
}

Check warning

Code scanning / PREfast

The const variable 'alignment_number_of_bytes' can be computed at compile-time. Consider using constexpr (con.5).

The const variable 'alignment_number_of_bytes' can be computed at compile-time. Consider using constexpr (con.5).

@yuslepukhin yuslepukhin Oct 31, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should beconstexpr

@yuslepukhin yuslepukhin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's address some important issues in this round first.

int input_idx,
/*out*/ bool& used_shared_buffers) override;

virtual std::optional<Tensor> GetPrePackTensor(int /*input_index*/) override;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

virtual is redundant in overriding functions. They are virtual by default.

size_t input_hidden_size, const T* weights_data,
size_t weight_matrix_col_size, PrePackedWeights* prepacked_weights);

void ConvertPrePackWeightsIntoTensor(onnxruntime::AllocatorPtr& alloc, const onnxruntime::Tensor& weights, PrePackedWeights* prepacked_weights);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going by the name prepacked_weights can not be optional parameter otherwise there is nothing to prepack, wen can it be nullptr?
Is it an input or output?


template <typename T>
void Attention<T>::ConvertTensorToPrePackWeight(void* tensor_data_raw) {
// buffer of packed_tensor is combine of:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create a struct of the layout and then reinterpret the ptr into it, it is difficult to read and understand the code.


TensorShapeVector shape_vector = weight_shape_.AsShapeVector();
size_t shape_vector_mem_size = utils::CalculateTensorShapeVectorMemoryUsage(shape_vector);
void* shape_vector_ptr = static_cast<void*>(&shape_vector);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is undefined behavior. You need to copy data not the object of this class.
For this you can just get GetDims() span and using it copy data from it.

std::memcpy(weight_shape_buffer.get(),
static_cast<char*>(tensor_data_raw) + 4 * sizeof(size_t),
weight_shape_buffer_size);
auto weight_shape_vector = static_cast<const InlinedVector<int64_t>*>(weight_shape_buffer.get());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just wrong. Undefined behavior.

Tensor ConvertPackedBufferAndShapeToTensor(onnxruntime::AllocatorPtr& alloc,
const onnxruntime::Tensor& weights,
size_t packed_weights_size_,
TensorShape weight_shape_,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Tensor shape is passed by value? Did you mean to pass span?
  2. Trailing underscores are reserved for member variables
  3. I am confused what is the result of this function? Is the last parameter also an output?

void* original_packed_buffer,
IAllocatorUniquePtr<void>& packed_buffer);

Tensor ConvertPackedBufferAndShapeToTensorWithFlag(onnxruntime::AllocatorPtr& alloc,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments for all other new functions.


size_t CalculateTensorShapeVectorMemoryUsage(TensorShapeVector& tensor_shape_vector) {
// Calculate memory for the vector object itself (metadata)
size_t vector_metadata_size = sizeof(std::vector<int64_t>);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not want to do it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is tensor_shape_vector related to std::vector?

// 3. original packed_weights buffer
TensorShapeVector shape_vector = weight_shape_.AsShapeVector();
size_t shape_vector_mem_size = utils::CalculateTensorShapeVectorMemoryUsage(shape_vector);
void* shape_vector_ptr = static_cast<void*>(&shape_vector);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined behavior

// 2. weight shape: first vector memory size, then vector content
// 3. original packed_weights buffer
TensorShapeVector shape_vector = weight_shape_.AsShapeVector();
size_t shape_vector_mem_size = utils::CalculateTensorShapeVectorMemoryUsage(shape_vector);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am struggling as to what you are trying to do here.

@snnn snnn closed this Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants