Skip to content

Commit

Permalink
test(llm): 添加 attention 前端单测
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <ydrml@hotmail.com>
  • Loading branch information
YdrMaster committed Feb 1, 2024
1 parent 45088cf commit 29790ed
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/08-01llm/test/test_attention.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../src/operators/attention.hh"
#include "llm/operators.h"
#include <gtest/gtest.h>

using namespace refactor;
using namespace llm;

TEST(infer, AttentionNoKvCache) {
llm::register_();
auto batch = DimExpr("N");
auto numHead = DimExpr(16);
auto seqLen = DimExpr(31);
auto headDim = DimExpr(64);
{
auto edges = Edges{
{Tensor::share(DataType::FP16, Shape{batch, numHead, seqLen, headDim}, {}), ""},
{Tensor::share(DataType::FP16, Shape{batch, numHead, seqLen, headDim}, {}), ""},
{Tensor::share(DataType::FP16, Shape{batch, numHead, seqLen, headDim}, {}), ""},
};
count_t inputs[]{0, 1, 2};
auto infered = Attention(0).infer(TensorRefs(edges, inputs), {true});
ASSERT_TRUE(infered.isOk());
auto outputs = std::move(infered.unwrap());
ASSERT_EQ(outputs.size(), 1);
auto y = std::move(outputs[0]);
ASSERT_EQ(y->dataType, DataType::FP16);
ASSERT_EQ(y->shape, edges[0].tensor->shape);
}
{
auto edges = Edges{
{Tensor::share(DataType::FP16, Shape{batch, numHead, seqLen, headDim}, {}), ""},
{Tensor::share(DataType::FP16, Shape{batch, DimExpr(4), seqLen, headDim}, {}), ""},
{Tensor::share(DataType::FP16, Shape{batch, DimExpr(4), seqLen, headDim}, {}), ""},
};
count_t inputs[]{0, 1, 2};
auto infered = Attention(0).infer(TensorRefs(edges, inputs), {true});
ASSERT_TRUE(infered.isOk());
auto outputs = std::move(infered.unwrap());
ASSERT_EQ(outputs.size(), 1);
auto y = std::move(outputs[0]);
ASSERT_EQ(y->dataType, DataType::FP16);
ASSERT_EQ(y->shape, edges[0].tensor->shape);
}
}

0 comments on commit 29790ed

Please sign in to comment.