Skip to content

Commit

Permalink
fix: qnn rope file name
Browse files Browse the repository at this point in the history
  • Loading branch information
liang1232018 committed Jan 4, 2024
1 parent 9b2ab1b commit 9e9bb8b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 98 deletions.
1 change: 1 addition & 0 deletions src/backends/QNN/op/QNNLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ErrorCode QNNLinear::setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<
for (int i = 0; i < 4; i++) {
dimensionsWeight[i] = weight_.shape()[i];
}
weight_.fullData(2.f);
qnnBackend_->modelAddTensor(weight_.name().c_str(), (Qnn_Tensor_t){
.version = QNN_TENSOR_VERSION_1,
{.v1 = {
Expand Down
79 changes: 78 additions & 1 deletion src/backends/QNN/op/QNNRoPE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,88 @@ QNNRoPE::QNNRoPE(Backend *bn, string opName) :
}

ErrorCode QNNRoPE::reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->head(), inputs[0]->sequence(), inputs[0]->dimension());

return NO_ERROR;
}

ErrorCode QNNRoPE::setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
return graphAddNode(name(), "RoPE", inputs, outputs, {}, "LLaMAOpPackageHtp");

pos_max_ = 16384;

ishape = inputs[0]->dimension();

if(!sin_.allocted()) {
if (pose_type_ == 1) {
sinusoidal_position_embedding_hf(1, 1, pos_max_, ishape, sin_, cos_);
} else if (pose_type_ == 2) {
sinusoidal_position_embedding(1, 1, pos_max_, ishape, sin_, cos_);
} else {
sinusoidal_position_embedding_hf(1, 1, pos_max_, ishape/2, sin_, cos_);
}
}

// vector<Tensor> qnn_RoPE_inputs;
// qnn_RoPE_inputs.push_back(inputs[0])
// qnn_RoPE_inputs.push_back(sin_);
// qnn_RoPE_inputs.push_back(cos_);

// return graphAddNode(name(), "RoPE", qnn_RoPE_inputs, outputs);

return NO_ERROR;
}


void QNNRoPE::sinusoidal_position_embedding(int batch_size, int nums_head, int seq_len, int output_dim, Tensor &sin, Tensor &cos) {
sin.reshape(batch_size, nums_head, seq_len, output_dim);
cos.reshape(batch_size, nums_head, seq_len, output_dim);
sin.alloc();
cos.alloc();
for (int n = 0; n < batch_size; ++n) {
for (int h = 0; h < nums_head; ++h) {
for (int s = 0; s < seq_len; ++s) {
for (int d = 0; d < output_dim; d += 2) {
int i = (int)d / 2;
float sin_value = std::sin(s / std::pow(10000, 2.0 * i / output_dim));
float cos_value = std::cos(s / std::pow(10000, 2.0 * i / output_dim));
sin.setDataAt<float>(n, h, s, d, sin_value);
cos.setDataAt<float>(n, h, s, d, cos_value);
if (d + 1 < output_dim) {
sin.setDataAt<float>(n, h, s, d + 1, sin_value);
cos.setDataAt<float>(n, h, s, d + 1, cos_value);
}
}
}
}
}
}

void QNNRoPE::sinusoidal_position_embedding_hf(int batch_size, int nums_head, int seq_len, int output_dim, Tensor &sin, Tensor &cos) {
sin.reshape(batch_size, nums_head, seq_len, output_dim);
cos.reshape(batch_size, nums_head, seq_len, output_dim);
sin.alloc();
cos.alloc();
for (int n = 0; n < batch_size; ++n) {
for (int h = 0; h < nums_head; ++h) {
for (int s = 0; s < seq_len; ++s) {
for (int d = 0; d < output_dim; d += 2) {
int i = (int)d;
if (d >= (int)output_dim / 2) {
i = (int)(d - output_dim / 2);
}
float sin_value = std::sin(s / std::pow(10000, 2.0 * i / output_dim));
float cos_value = std::cos(s / std::pow(10000, 2.0 * i / output_dim));
sin.setDataAt<float>(n, h, s, d, sin_value);
cos.setDataAt<float>(n, h, s, d, cos_value);
if (d + 1 < output_dim) {
sin.setDataAt<float>(n, h, s, d + 1, sin_value);
cos.setDataAt<float>(n, h, s, d + 1, cos_value);
}
}
}
}
}
}

} // namespace mllm

96 changes: 0 additions & 96 deletions src/backends/QNN/op/QNNRope.cpp

This file was deleted.

1 change: 0 additions & 1 deletion src/backends/QNN/op/QNNSiLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ QNNSiLU::QNNSiLU(Backend *bn, string opName) :
}

ErrorCode QNNSiLU::reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
<<<<<<< HEAD
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->head(), inputs[0]->sequence(), inputs[0]->dimension());
return Op::reshape(inputs, outputs);
}
Expand Down

0 comments on commit 9e9bb8b

Please sign in to comment.