mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge pull request #27238 from nklskyoy:mha-rope
Rotary positional embeddings #27238 Rotary positional embeddings let an attention block encode the relative position between tokens. Widely adopted in LLMs such as LLaMA—and equally applicable to Vision Transformers—the ONNX Runtime com.microsoft.Attention operator already exposes this via its `do_rotary` flag (see docs), and this PR adds support for that flag in OpenCV. _Operator spec: https://github.com/microsoft/onnxruntime/blob/v1.16.1/docs/ContribOperators.md#com.microsoft.Attention_ Materials: - RoFormer paper https://arxiv.org/abs/2104.09864 - RoPe for Vision Transformer https://github.com/naver-ai/rope-vit - llama implementation https://github.com/meta-llama/llama/blob/main/llama/model.py#L80 Merge with https://github.com/opencv/opencv_extra/pull/1250 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -743,6 +743,32 @@ TEST_F(Layer_RNN_Test, get_set_test)
|
||||
EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
|
||||
}
|
||||
|
||||
TEST(Layer_MHARoPe_Test_Accuracy_with_, Pytorch)
|
||||
{
|
||||
Mat QKV = blobFromNPY(_tf("mha_rope.QKV.npy"));
|
||||
Mat QKV_bias = blobFromNPY(_tf("mha_rope.QKV_bias.npy"));
|
||||
std::vector<int> qkv_hidden_sizes = { 256, 256, 256 };
|
||||
LayerParams mhaParams;
|
||||
mhaParams.blobs.resize(2);
|
||||
mhaParams.blobs[0] = QKV;
|
||||
mhaParams.blobs[1] = QKV_bias;
|
||||
mhaParams.set("num_heads", 4);
|
||||
mhaParams.set(
|
||||
"qkv_hidden_sizes",
|
||||
DictValue::arrayInt(&qkv_hidden_sizes[0], qkv_hidden_sizes.size())
|
||||
);
|
||||
mhaParams.set("do_rotary", true);
|
||||
|
||||
Ptr<AttentionLayer> layer = AttentionLayer::create(mhaParams);
|
||||
Mat inp = blobFromNPY(_tf("mha_rope.input.npy"));
|
||||
std::vector<Mat> inputs(1, inp), outputs;
|
||||
runLayer(layer, inputs, outputs);
|
||||
Mat h_t_reference = blobFromNPY(_tf("mha_rope.output.npy"));
|
||||
normAssert(h_t_reference, outputs[0]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_P(Test_Caffe_layers, Accum)
|
||||
{
|
||||
#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF
|
||||
|
||||
Reference in New Issue
Block a user