Caffe源码(caffe version:09868ac , date: 2015.08.15)中有一些重要文件,这里介绍下math_functions文件。
1. include文件:
(1)、<glog/logging.h>:GLog库,它是google的一个开源的日志库,其使用可以参考:http://blog.****.net/fengbingchun/article/details/48768039;
(2)、<caffe/common.hpp>、<caffe/util/device_alternate.hpp>:这两个文件的介绍可以参考: http://blog.****.net/fengbingchun/article/details/54955236 ;
2. <caffe/util/mkl_alternate.hpp>文件:
这个文件里包含两种库,一个是Intel MKL,一个是OpenBLAS,这里用的是OpenBLAS。如果商用Intel MKL是需要付费的,下面仅对Intel MKL进行简单介绍。
Intel MKL(Math Kernel Library)即Intel数学核心函数库,它是一套高度优化和广泛线程安全的数学例程,专为需要极致性能的科学、工程及金融等领域的应用而设计。核心数学函数包括BLAS、LAPACK、ScaLAPACK、Sparse Solver、快速傅里叶变换、矢量数学及其它函数。它可以为当前及下一代英特尔处理器提供性能优化,包括更出色地与Microsoft Visual Studio、Eclipse和XCode相集成。英特尔MKL支持完全集成英特尔兼容性OpenMP运行时库,以实现更出色的Windows/Linux跨平台兼容性。
关于OpenBLAS的介绍可以参考: http://blog.****.net/fengbingchun/article/details/55509764;
在github/fengbingchun/Caffe_Test中<mkl_alternate.hpp>中走的是OpenBLAS分支。
(1)、定义了一些宏:
DEFINE_VSL_UNARY_FUNC:一元函数,包括Sqr、Exp、Ln、Abs,对应的函数为vsSqr、vsExp、vsLn、vsAbs、vdSqr、vdExp、vdLn、vdAbs,支持float和double类型。
DEFINE_VSL_UNARY_FUNC_WITH_PARAM:带一个参数的一元函数,包括Powx,对应的函数为vsPowx、vdPowx,支持float和double类型。
DEFINE_VSL_BINARY_FUNC:二元函数,包括Add、Sub、Mul、Div,对应的函数为vsAdd、vsSub、vsMul、vsDiv、vdAdd、vdSub、vdMul、vdDiv,支持float和double类型。
(2)、定义了axpby函数,支持两种类型,cblas_saxpby、cblas_daxpby,如果设置incX和incY为1(即步长为1),则:Y=alpha*X+beta*Y
mkl_alternate文件测试代码如下:
int test_caffe_util_mkl_alternate() { const int N{ 5 }; float a[N] {1, 2, 3, 4, 5}, b{ 2 }, alpha{ 0.2f }, beta{0.4f}; float y1[N], y2[N], y3[N], y4[N]{6, 7, 8, 9, 10}; fprintf(stderr, "test unary function: vsSqr\n"); vsSqr(N, a, y1); for (auto ret : y1) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test function unary function with singular parameter: vsPowx\n"); vsPowx(N, a, b, y2); for (auto ret : y2) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test binary function: vsAdd\n"); vsAdd(N, a, a, y3); for (auto ret : y3) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test axpby function(Y=alpha*X+beta*Y): cblas_saxpby\n"); cblas_saxpby(N, alpha, a, 1, beta, y4, 1); for (auto ret : y4) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); return 0; }
执行结果如下:
3. math_functions文件内函数:封装了一些基础的数学运算函数
(1)、caffe_cpu_gemm:C=alpha*A*B+beta*C;
(2)、caffe_cpu_gemv:y=alpha*A*x+beta*y;
(3)、caffe_axpy:Y=alpha*X+Y;
(4)、caffe_cpu_axpby:Y=alpha*X+beta*Y;
(5)、caffe_copy:从X中拷贝前N个元素到Y中;
(6)、caffe_set:将X中的前N个元素置为alpha;
(7)、caffe_add_scalar:给Y中的前N个元素分别加上常数alpha;
(8)、caffe_scal:X = alpha*X;
(9)、caffe_sqr/ caffe_exp/caffe_log/caffe_abs:会调用mkl_alternate.hpp中的vsSqr、vsExp、vsLn、vsAbs、vdSqr、vdExp、vdLn、vdAbs函数;
(10)、caffe_add/caffe_sub/caffe_mul/caffe_div:会调用mkl_alternate.hpp中的vsAdd、vsSub、vsMul、vsDiv、vdAdd、vdSub、vdMul、vdDiv函数;
(11)、caffe_powx:会调用mkl_alternate.hpp中的vsPowx和vdPowx函数;
(12)、caffe_rng_rand:返回一个unsignedint类型的随机数;
(13)、caffe_nextafter:在最大方向上,返回b可以表示的最接近的数值;
(14)、caffe_rng_uniform:产生指定范围内的均匀分布随机数;
(15)、caffe_rng_gaussian:产生高斯分布随机数;
(16)、caffe_rng_bernoulli:产生伯努利分布随机数;
(17)、caffe_cpu_dot:计算步长为1的内积;
(18)、caffe_cpu_strided_dot:计算指定步长的内积;
(19)、caffe_cpu_hamming_distance:计算x、y之间的海明距离;
(20)、caffe_cpu_asum:计算向量x中前n个元素的绝对值之和;
(21)、caffe_sign:类似于正负号函数,仅返回-1或1;
(22)、caffe_cpu_scale:Y=alpha*X 。
4. 宏DEFINE_CAFFE_CPU_UNARY_FUNC:一元函数,类似于mkl_alternate.hpp中的宏DEFINE_VSL_UNARY_FUNC,包括:
(1)、caffe_cpu_sign:正负号函数,输出-1、0、1;
(2)、caffe_cpu_sgnbit:作用类似于std::signbit,static_cast<bool>((std::signbit)(x));x为负数输出为1,其它输出为0;
(3)、caffe_cpu_fabs:取绝对值,作用类似于std::fabs。
math_functions文件测试代码如下:
int test_caffe_util_math_functions() { float alpha{ 0.5f }, beta{ 0.1f }; // h*w: A: 2*3; B: 3*4; C: 2*4 float A[2 * 3] {1, 2, 3, 4, 5, 6}, B[3 * 4] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, y1[2 * 4]{1, 2, 3, 4, 5, 6, 7, 8}, x[3]{1, 2, 3}, y2[2]{1, 2}, y3[6] {1, 2, 3, 4, 5, 6}, y4[6] {1, 2, 3, 4, 5, 6}, y7[6]{1, 2, 3, 4, 5, 6}, y10[6] {1, 2, 3, 4, 5, 6}, y11[6] {1, 2, 3, 4, 5, 6}, C[6] {-2, -1, 0, 1, 2, 3}, y19[6] {-10, -10, -10, -10, -10, -10}; float y5[6], y6[6], y20[6], y21[6], y22[6]; int y12[6] {1, 2, 3, 4, 5, 6}; fprintf(stderr, "test math function: caffe_cpu_gemm(C=alpha*A*B+beta*C)\n"); // A、B、y1: matrix caffe::caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, 2, 4, 3, alpha, A, B, beta, y1); for (auto ret : y1) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_gemv(y=alpha*A*x+beta*y)\n"); // A: matrix; x、y2: vector caffe::caffe_cpu_gemv(CblasNoTrans, 2, 3, alpha, A, x, beta, y2); for (auto ret : y2) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_axpy(Y=alpha*X+Y)\n"); caffe::caffe_axpy(6, alpha, A, y3); for (auto ret : y3) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_axpby(Y= alpha*X+beta*Y)\n"); caffe::caffe_cpu_axpby(6, alpha, A, beta, y4); for (auto ret : y4) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_copy\n"); caffe::caffe_copy(3, A, y5); for (auto ret : y5) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_set\n"); caffe::caffe_set(3, alpha, y6); for (auto ret : y6) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_scal(X=alpha*X)\n"); caffe::caffe_scal(4, alpha, y7); for (auto ret : y7) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_rng_rand\n"); unsigned int y8 = caffe::caffe_rng_rand(); fprintf(stderr, "caffe rng rand: %d\n", y8); fprintf(stderr, "test math function: caffe_nextafter\n"); float y9 = caffe::caffe_nextafter(alpha); fprintf(stderr, " caffe next after: %f\n", y9); fprintf(stderr, "test math function: caffe_rng_uniform\n"); caffe::caffe_rng_uniform(4, -2.f, 2.f, y10); for (auto ret : y10) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_rng_gaussian\n"); caffe::caffe_rng_gaussian(4, -2.f, alpha, y11); for (auto ret : y11) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_rng_bernoulli\n"); caffe::caffe_rng_bernoulli(4, alpha, y12); for (auto ret : y12) { fprintf(stderr, "%d ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_dot\n"); float y13 = caffe::caffe_cpu_dot(3, A, B); fprintf(stderr, "caffe cpu dot: %f\n", y13); fprintf(stderr, "test math function: caffe_cpu_strided_dot\n"); float y14 = caffe::caffe_cpu_strided_dot(2, A, 2, B, 2); fprintf(stderr, "caffe cpu strided dot: %f\n", y14); fprintf(stderr, "test math function: caffe_cpu_hamming_distance\n"); int y15 = caffe::caffe_cpu_hamming_distance(4, A, C); fprintf(stderr, "caffe cpu hamming distance: %d\n", y15); fprintf(stderr, "test math function: caffe_cpu_asum\n"); float y16 = caffe::caffe_cpu_asum(5, C); fprintf(stderr, "caffe cpu asum: %f\n", y16); fprintf(stderr, "test math function: caffe_sign\n"); int8_t y17 = caffe::caffe_sign(-10.0f); int8_t y18 = caffe::caffe_sign(10.0f); fprintf(stderr, "caffe sign: -10.0f: %d, 10.0f: %d\n", y17, y18); fprintf(stderr, "test math function: caffe_cpu_scale\n"); caffe::caffe_cpu_scale(5, alpha, C, y19); for (auto ret : y19) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_sign\n"); caffe::caffe_cpu_sign(5, C, y20); for (auto ret : y20) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_sgnbit\n"); caffe::caffe_cpu_sgnbit(5, C, y21); for (auto ret : y21) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); fprintf(stderr, "test math function: caffe_cpu_fabs\n"); caffe::caffe_cpu_fabs(5, C, y22); for (auto ret : y22) { fprintf(stderr, "%f ", ret); } fprintf(stderr, "\n"); return 0; }
执行结果如下: