forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarginCriterion.cu
45 lines (37 loc) · 1.06 KB
/
MarginCriterion.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "THCUNN.h"
#include "common.h"
#include "TH/THHalf.h"
#include "THCHalfAutoNumerics.cuh"
#include <thrust/fill.h>
#include <thrust/functional.h>
#include <thrust/device_ptr.h>
#include <thrust/reduce.h>
#include <thrust/inner_product.h>
template <typename Dtype, typename Acctype>
struct margin_functor
{
margin_functor(Acctype margin)
: margin(margin)
{}
__host__ __device__ Acctype operator()(const Dtype &x, const Dtype &y) const
{
Acctype z = margin - ScalarConvert<Dtype, Acctype>::to(x) * y;
return z >= 0 ? z : 0;
}
const Acctype margin;
};
template <typename Dtype, typename Acctype>
struct margin_updateGradInput_functor
{
const Acctype margin, norm;
margin_updateGradInput_functor(Acctype margin_, Acctype norm_)
: margin(margin_)
, norm(norm_)
{}
__host__ __device__ Dtype operator()(const Dtype &x, const Dtype &y) const
{
return ScalarConvert<Acctype, Dtype>::to((ScalarConvert<Dtype, Acctype>::to(x) * y) < margin ? -norm * y : 0);
}
};
#include "generic/MarginCriterion.cu"
#include "THCGenerateFloatTypes.h"