Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tmp.h « functional « src - github.com/marian-nmt/marian.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65cb49d8b8a060fe1508184815b1ce9520dc988e (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once

#include "functional/defs.h"
#include "functional/tensor.h"
#include "functional/array.h"

namespace marian {
namespace functional {

template <size_t K, class Functor>
struct FApply {};

template <class Functor>
struct FApply<1, Functor> {
  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 1>& in,
      const functional::Array<int, 1>& indices) {
    return functor(in[0][indices[0]]);
  }

  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 1>& in,
      int index) {
    return functor(in[0][index]);
  }
};

template <class Functor>
struct FApply<2, Functor> {
  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 2>& in,
      const functional::Array<int, 2>& indices) {
    return functor(in[0][indices[0]], in[1][indices[1]]);
  }

  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 2>& in,
      int index) {
    return functor(in[0][index], in[1][index]);
  }
};

template <class Functor>
struct FApply<3, Functor> {
  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 3>& in,
      const functional::Array<int, 3>& indices) {
    return functor(in[0][indices[0]], in[1][indices[1]], in[2][indices[2]]);
  }

  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 3>& in,
      int index) {
    return functor(in[0][index], in[1][index], in[2][index]);
  }
};

template <class Functor>
struct FApply<4, Functor> {
  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 4>& in,
      const functional::Array<int, 4>& indices) {
    return functor(in[0][indices[0]],
                   in[1][indices[1]],
                   in[2][indices[2]],
                   in[3][indices[3]]);
  }

  __HDI__ static float apply(
      Functor functor,
      functional::Array<functional::Tensor<float>, 4>& in,
      int index) {
    return functor(in[0][index], in[1][index], in[2][index], in[3][index]);
  }
};

template <size_t K, class Functor>
__HDI__ float apply(Functor functor,
                    functional::Array<functional::Tensor<float>, K>& in,
                    const functional::Array<int, K>& indices) {
  return FApply<K, Functor>::apply(functor, in, indices);
}

template <size_t K, class Functor>
__HDI__ float apply(Functor functor,
                    functional::Array<functional::Tensor<float>, K>& in,
                    int index) {
  return FApply<K, Functor>::apply(functor, in, index);
}

/******************************************************************************/

template <size_t n, size_t N, size_t K>
struct Loop {
  template <class Functor>
  __HDI__ static float result(
      Functor functor,
      functional::Array<functional::Tensor<float>, K>& in,
      const functional::Array<int, K>& pAcc,
      const functional::Array<int, N>& length,
      const functional::Array<int, N>& dim) {
    float sum = 0;
    functional::Array<int, K> acc;
    for(int i = 0; i < length[N - n]; ++i) {
      for(int j = 0; j < K; ++j) {
        acc[j] = pAcc[j] + (dim[N - n] + i) * in[j].shape().bstride(N - n);
      }
      sum += Loop<n - 1, N, K>::result(functor, in, acc, length, dim);
    }
    return sum;
  }
};

template <size_t N, size_t K>
struct Loop<1, N, K> {
  template <class Functor>
  __HDI__ static float result(
      Functor functor,
      functional::Array<functional::Tensor<float>, K>& in,
      const functional::Array<int, K>& pAcc,
      const functional::Array<int, N>& length,
      const functional::Array<int, N>& dim) {
    float sum = 0;
    functional::Array<int, K> acc;
    for(int i = 0; i < length[N - 1]; ++i) {
      for(int j = 0; j < K; ++j) {
        acc[j] = pAcc[j] + (dim[N - 1] + i) * in[j].shape().bstride(N - 1);
      }
      sum += apply<K>(functor, in, acc);
    }
    return sum;
  }
};

template <size_t N, size_t K, class Functor>
__HDI__ float loops(Functor functor,
                    functional::Array<functional::Tensor<float>, K>& in,
                    const functional::Array<int, N>& length,
                    const functional::Array<int, N>& dim) {
  functional::Array<int, K> acc = {0};
  return Loop<N, N, K>::result(functor, in, acc, length, dim);
}
}
}