Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matmul cant't get expected values when input is 5d or above #270

Open
ahqzy opened this issue Jan 17, 2022 · 1 comment
Open

Matmul cant't get expected values when input is 5d or above #270

ahqzy opened this issue Jan 17, 2022 · 1 comment
Milestone

Comments

@ahqzy
Copy link

ahqzy commented Jan 17, 2022

Hi,
I'm testing matmul in timvx(opencl). It works well when both input is 4d or below. But it works abnormally when input dimension is equal or greater than 5. For example, I wrote this test cpp file:

#include "tim/vx/context.h"
#include "tim/vx/graph.h"
#include "tim/vx/ops/matmul.h"
#include "test_utils.h"
#include "gtest/gtest.h"

TEST(Matmul, shape_2_2_2_3_2_shape_2_3_float) {
auto ctx = tim::vx::Context::Create();
auto graph = ctx->CreateGraph();

tim::vx::ShapeType a_shape({2, 3, 2, 2, 2});
tim::vx::ShapeType b_shape({3, 2});
tim::vx::ShapeType out_shape({3, 3, 2, 2, 2});
tim::vx::TensorSpec a_spec(tim::vx::DataType::FLOAT32,
                a_shape, tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec b_spec(tim::vx::DataType::FLOAT32,
                b_shape, tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32,
                        out_shape, tim::vx::TensorAttribute::OUTPUT);

auto a_tensor = graph->CreateTensor(a_spec);
auto b_tensor = graph->CreateTensor(b_spec);
auto out_tensor = graph->CreateTensor(out_spec);

std::vector<float> a_data = {
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2
};
std::vector<float> b_data = {
	2, 2, 2,
	3, 3, 3	
};
	
std::vector<float> golden = {
    5, 5,  5,
    5, 5, 5,
    5, 5, 5,
    10, 10,  10,
    10, 10, 10,
    10, 10, 10,

	5, 5, 5,
    5, 5, 5,
    5, 5, 5,
    10, 10,  10,
    10, 10, 10,
    10, 10, 10,

	5, 5, 5,
    5, 5, 5,
    5, 5, 5,
    10, 10,  10,
    10, 10, 10,
    10, 10, 10,

	5, 5, 5,
    5, 5, 5,
    5, 5, 5,
    10, 10,  10,
    10, 10, 10,
    10, 10, 10,
};

EXPECT_TRUE(a_tensor->CopyDataToTensor(a_data.data(), a_data.size() * sizeof(float)));
EXPECT_TRUE(b_tensor->CopyDataToTensor(b_data.data(), b_data.size() * sizeof(float)));

auto op = graph->CreateOperation<tim::vx::ops::Matmul>(false, false);
(*op).BindInputs({a_tensor, b_tensor}).BindOutputs({out_tensor});

EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());

std::vector<float> output(golden.size() * sizeof(float));
EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data()));

for (size_t i = 0; i < golden.size(); ++i){
  std::cout<<"i: " <<i<<", golen: "<<golden[i]<<", output: "<<output[i]<<std::endl;
	}

EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f));

}

TEST(Matmul, shape_2_2_2_3_2_shape_2_float) {
auto ctx = tim::vx::Context::Create();
auto graph = ctx->CreateGraph();

tim::vx::ShapeType a_shape({2, 3, 2, 2, 2});
tim::vx::ShapeType b_shape({2});
tim::vx::ShapeType out_shape({3, 2, 2, 2});
tim::vx::TensorSpec a_spec(tim::vx::DataType::FLOAT32,
                a_shape, tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec b_spec(tim::vx::DataType::FLOAT32,
                b_shape, tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32,
                        out_shape, tim::vx::TensorAttribute::OUTPUT);

auto a_tensor = graph->CreateTensor(a_spec);
auto b_tensor = graph->CreateTensor(b_spec);
auto out_tensor = graph->CreateTensor(out_spec);

std::vector<float> a_data = {
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2,
    1, 1,
    1, 1,
    1, 1,
    2, 2,
    2, 2,
    2, 2
};
std::vector<float> b_data = {
	5, 6,
};
	
std::vector<float> golden = {
    11, 11,  11,
    22, 22, 22,
	11, 11,  11,
	22, 22, 22,
    11, 11,  11,
    22, 22, 22,
    11, 11,  11,
    22, 22, 22,
};

EXPECT_TRUE(a_tensor->CopyDataToTensor(a_data.data(), a_data.size() * sizeof(float)));
EXPECT_TRUE(b_tensor->CopyDataToTensor(b_data.data(), b_data.size() * sizeof(float)));

auto op = graph->CreateOperation<tim::vx::ops::Matmul>(false, false);
(*op).BindInputs({a_tensor, b_tensor}).BindOutputs({out_tensor});

EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());

std::vector<float> output(golden.size() * sizeof(float));
EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data()));

for (size_t i = 0; i < golden.size(); ++i){
  std::cout<<"i: " <<i<<", golen: "<<golden[i]<<", output: "<<output[i]<<std::endl;
	}

EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f));

}

int main(int argc, char** argv)

{

	::testing::InitGoogleTest(&argc, argv);
	 
		return RUN_ALL_TESTS();

}

In the example, I want to test 2 cases:5dx2d and 5dx1d,but I got errors both, like this:

The difference between expected[i] and actual[i] is 5, which exceeds abs_error, where
expected[i] evaluates to 5,
actual[i] evaluates to 0, and
abs_error evaluates to 9.9999997473787516e-06.
at index:0
../test_utils.h:118: Failure


Can anybody help me?

@sunshinemyson
Copy link
Contributor

@ahqzy ,

Thanks for your report, We will check it ASAP.

@sunshinemyson sunshinemyson added this to the 22Q4_rel milestone Sep 28, 2022
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

No branches or pull requests

2 participants