What does caffe_cpu_sign() do and where is it?

564 views
Skip to first unread message

Kun Wang

unread,
Oct 7, 2015, 10:05:27 AM10/7/15
to Caffe Users
Hi all,

This might be a silly question, but I was wondering what does the function caffe_cpu_sign() do? I cannot find the definition of it, or even declaration of it in Caffe!

I can find the declaration of caffe_gpu_sign() in math_functions.hpp, however, no definition, eigher.

Can anyone help me?

Thanks!

Kun

Prem kumar

unread,
Mar 17, 2016, 12:16:02 AM3/17/16
to Caffe Users
Hi Kun Wang, Did you find the solution for this? I'm also wondering what that method does. I believe they complied the .cpp file and added to the path somewhere, since we wont need that to modify.

Thanks
Prem

Lijun Wang

unread,
Aug 19, 2016, 7:31:13 AM8/19/16
to Caffe Users
The function caffe_cpu_sign() is defined in the math_function.hpp as follows:

template<typename Dtype>
inline int8_t caffe_sign(Dtype val) {
  return (Dtype(0) < val) - (val < Dtype(0));
}

// The following two macros are modifications of DEFINE_VSL_UNARY_FUNC
//   in include/caffe/util/mkl_alternate.hpp authored by @Rowland Depp.
// Please refer to commit 7e8ef25c7 of the boost-eigen branch.
// Git cherry picking that commit caused a conflict hard to resolve and
//   copying that file in convenient for code reviewing.
// So they have to be pasted here temporarily.
#define DEFINE_CAFFE_CPU_UNARY_FUNC(name, operation) \
  template<typename Dtype> \
  void caffe_cpu_##name(const int n, const Dtype* x, Dtype* y) { \
    CHECK_GT(n, 0); CHECK(x); CHECK(y); \
    for (int i = 0; i < n; ++i) { \
      operation; \
    } \
  }

// output is 1 for the positives, 0 for zero, and -1 for the negatives
DEFINE_CAFFE_CPU_UNARY_FUNC(sign, y[i] = caffe_sign<Dtype>(x[i]));


Similarly, the GPU version is implemented by the following macros in math_function.hpp:
#define DEFINE_AND_INSTANTIATE_GPU_UNARY_FUNC(name, operation) \
template<typename Dtype> \
__global__ void name##_kernel(const int n, const Dtype* x, Dtype* y) { \
  CUDA_KERNEL_LOOP(index, n) { \
    operation; \
  } \
} \
template <> \
void caffe_gpu_##name<float>(const int n, const float* x, float* y) { \
  /* NOLINT_NEXT_LINE(whitespace/operators) */ \
  name##_kernel<float><<<CAFFE_GET_BLOCKS(n), CAFFE_CUDA_NUM_THREADS>>>( \
      n, x, y); \
} \
template <> \
void caffe_gpu_##name<double>(const int n, const double* x, double* y) { \
  /* NOLINT_NEXT_LINE(whitespace/operators) */ \
  name##_kernel<double><<<CAFFE_GET_BLOCKS(n), CAFFE_CUDA_NUM_THREADS>>>( \
      n, x, y); \
}

and defined in math_function.cu as:
DEFINE_AND_INSTANTIATE_GPU_UNARY_FUNC(sign, y[index] = (Dtype(0) < x[index])
                                      - (x[index] < Dtype(0)));


Lijun




在 2016年3月17日星期四 UTC+8下午12:16:02,Prem kumar写道:
Reply all
Reply to author
Forward
0 new messages