Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7003 ディスカッション

Some questions about using MKL for high-dimensional convolution

Chen_muyu
初心者
2,075件の閲覧回数

I want to use mkl to implement simple 3D convolution, for example, my input data size is 10*450*2456, I use a 5*5*5 convolution kernel, the output data size should be 6*446*2452, I use these parameters, refer to the official sample code, but it does't works. I have been using the vsldConvExecX command, it  return an uncertain integer, I think the official example code and documentation for how to set the step and code instructions are very vague, hope to add some content to explain more clearly, thank you! By the way,  I would appreciate it, if I can get the code for my calculation example.

屏幕截图 2024-03-11 181335.png

0 件の賞賛
1 解決策
Chen_muyu
初心者
1,634件の閲覧回数

I finally finally found my error, which came from my carelessness:

status = vsldConvExecX(&my_task, m_filter, NULL, m_output, NULL);

This sentence does not need to take the address"&", but I wrote it and did not report errors, so it has been ignored by me.

One thing to note, this is required for functions that create tasks:

int status = vsldConvNewTaskX(&my_task, VSL_CONV_MODE_AUTO, 3, inputDims, filterDims, outputDims, m_input, NULL);

I also learned from the official samples that if there is no special stride and padding required, NULL can be used directly

Now my script is running well, thank you for being so patient to help me solve my problem, wish you happy every day!!!⊂◉‿◉つ

元の投稿で解決策を見る

16 返答(返信)
Mahan
モデレーター
2,021件の閲覧回数

Hi Chen,


Would it be possible for you to attach the source code here instead of the snippet.


Mahan
モデレーター
1,994件の閲覧回数

Hi Chen,

A gentle reminder for my previous message.


Chen_muyu
初心者
1,915件の閲覧回数

 

 
 
Mahan
モデレーター
1,891件の閲覧回数

Hi Chen,

Is the issue resolved, you marked my previous message, where I asked for the source code, as a solution.


Chen_muyu
初心者
1,876件の閲覧回数

    Sorry it took so long to get back to you;<(。_。)>  

    My problem has not been solved, it was my fault to set it to be solved; I just tried cudnn to solve my problem, it worked well (but my 8G GPU memory is running out), so I still want to use the CPU to calculate convolution( just  like the MATLAB). I took a moment to rewrite the code with it below, thanks very much for answering my question and pointing out my errors.

 

// use_MKL_convn_test.cpp 
 
#include <iostream>
#include"mkl.h"
#include"opencv2/opencv.hpp"
 
int main() {
int inputDims[3]{ 10,450,2456 };// 输入张量的尺寸
int filterDims[3]{ 5,5,5 };// 卷积核尺寸
int outputDims[3]{ 6,446,2452 }; //输出张量的尺寸
int xstride[3]{ 1,1,1 };
int ystride[3]{ 1,1,1 };
int zstride[3]{ 1,1,1 };
double* output{ nullptr };
VSLConvTaskPtr my_task;//卷积任务句柄
cv::Mat m_input = cv::Mat::ones(1,inputDims[0]* inputDims[1]* inputDims[2],CV_32F);//10*450*2456
std::vector<double> m_one_filter_data(filterDims[0] * filterDims[1] * filterDims[2],1.0f);//5*5*5
cv::Mat m_output;//should be 6*446*2452
 
int status = vsldConvNewTaskX(&my_task, VSL_CONV_MODE_AUTO, 3, inputDims, filterDims, outputDims, m_input.ptr<double>(0), xstride);
if (status != VSL_STATUS_OK) {
std::cerr << "Failed to create convolution handle, code: " << status << std::endl;
return -1;
}
output = new double[outputDims[0] * outputDims[1] * outputDims[2]];
status = vsldConvExecX(&my_task, m_one_filter_data.data(), ystride, output, zstride);
if (status != VSL_STATUS_OK) {
std::cerr << "Failed to do convolution, code:" << status << std::endl;
delete[] output;
return status;
}
std::cout << "卷积计算成功" << std::endl;
//将output数组以Mat形式返回;
m_output = cv::Mat(1, outputDims[0] * outputDims[1] * outputDims[2], CV_64F, output).clone();
delete[] output;
 
return 0;
}
Mahan
モデレーター
1,858件の閲覧回数

Hi Chen,


Thanks for sharing the script.

If you are using make/cmake, could you please also share the Make/CMakeList for this?


Chen_muyu
初心者
1,852件の閲覧回数

I didn't use cmake to build my project, I just created a new empty project in visual studio, my include file was correct, and I used Intel Libraries for OneAPI in VS for mkl configuration.As shown in Figure 1 below. When I run my script, the error code looks like Figure 2, and I can't find the cause of the error in the error code's documentation.

屏幕截图 2024-03-28 142357.png屏幕截图 2024-03-28 142440.png

Mahan
モデレーター
1,762件の閲覧回数

Hi Chen,

Please us ethe latest oneAPI 2024.1

Also please do the following changes in the properties as shown in the attachment and let me know what you find.

 

Chen_muyu
初心者
1,753件の閲覧回数

I have 2024.0 installed now, do I need to uninstall or something before installing 2024.1

Chen_muyu
初心者
1,744件の閲覧回数

Surprisingly, I updated it using the APP: "Intel oneAPI base Toolkit" ,and didn't need to worry about uninstalling older version.

ɷ◡ɷ

Mahan
モデレーター
1,720件の閲覧回数

Yes, that is correct. You do not need to uninstall; just updating the app will do.


Chen_muyu
初心者
1,716件の閲覧回数

yep,I have completed your previous suggestion, but my problem now is that when I execute vsldConvExecX, It will report an error of crossing the boundary. Is my stride wrong?

My code is as follows:

#include <iostream>
#include"mkl.h"
#include"opencv2/opencv.hpp"
 
int main() {
const MKL_INT inputDims[3]{ 10,450,2456 };// 输入张量的尺寸
const MKL_INT filterDims[3]{ 5,5,5 };// 卷积核尺寸
const MKL_INT outputDims[3]{ inputDims[0] + filterDims[0] - 1,
inputDims[1] + filterDims[1] - 1, 
inputDims[2] + filterDims[2] - 1 }; //输出张量的尺寸:14*454*2460
const MKL_INT xstride[3]{ 1,1,1 };
const MKL_INT ystride[3]{ 1,1,1 };
const MKL_INT zstride[3]{ 1,1,1 };
VSLConvTaskPtr my_task;//卷积任务句柄
//Using ordinary arrays
double* m_input = new double[inputDims[0] * inputDims[1] * inputDims[2]];
double* m_filter = new double[filterDims[0] * filterDims[1] * filterDims[2]];
double* m_output = new double[outputDims[0] * outputDims[1] * outputDims[2]];
for (int i = 0; i < inputDims[0] * inputDims[1] * inputDims[2]; i++)m_input[i] = 1.0;
for (int i = 0; i < filterDims[0] * filterDims[1] * filterDims[2]; i++)m_filter[i] = 1.0;
 
int status = vsldConvNewTaskX(&my_task, VSL_CONV_MODE_AUTO, 3, inputDims, filterDims, outputDims, m_input, xstride);
if (status != VSL_STATUS_OK) {
std::cerr << "Failed to create convolution handle, code: " << status << std::endl;
return -1;
}
 
status = vsldConvExecX(&my_task, m_filter, ystride, m_output, zstride);
if (status != VSL_STATUS_OK) {
std::cerr << "Failed to do convolution, code:" << status << std::endl;
 
delete[] m_input, m_filter,m_output;
return status;
}
std::cout << "卷积计算成功" << std::endl;
for (int i = 0; i < 100; i++) {
std::cout << m_output[i] << std::endl;
}
delete[] m_input, m_filter, m_output;
return 0;
}
Chen_muyu
初心者
1,692件の閲覧回数

I read the references to Convolution and Correlation Data Allocation carefully:https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-c/2024-1/convolution-and-correlation-data-allocation.html#GUID-EC3E2E6B-4989-4AF1-A3A8-7BAE7C945E05 ,and I modified my stride to:

const MKL_INT xstride[3]{ inputDims[1]* inputDims[2],inputDims[2],1};
const MKL_INT ystride[3]{ filterDims[1] * filterDims[2],filterDims[2],1 };
const MKL_INT zstride[3]{ outputDims[1] * outputDims[2],outputDims[2],1 };

 

but it doesn't help, the error is the same as the previous one.

Mahan
モデレーター
1,649件の閲覧回数

Hi Chen,


This is the formula for the output dimension, make sure this is correct.

[(W−K+2P)/S]+1.

  • W is the input size
  • K is the Kernel size
  • P is the padding
  • S is the stride



Chen_muyu
初心者
1,635件の閲覧回数

I finally finally found my error, which came from my carelessness:

status = vsldConvExecX(&my_task, m_filter, NULL, m_output, NULL);

This sentence does not need to take the address"&", but I wrote it and did not report errors, so it has been ignored by me.

One thing to note, this is required for functions that create tasks:

int status = vsldConvNewTaskX(&my_task, VSL_CONV_MODE_AUTO, 3, inputDims, filterDims, outputDims, m_input, NULL);

I also learned from the official samples that if there is no special stride and padding required, NULL can be used directly

Now my script is running well, thank you for being so patient to help me solve my problem, wish you happy every day!!!⊂◉‿◉つ

Mahan
モデレーター
1,612件の閲覧回数

Hi Chen,


I am glad that you have found the solution. Have a great time.


返信