Repository navigation
Expand file tree
/
Copy pathlinear-gpu-ref-ops.h
More file actions
174 lines (135 loc) · 4.22 KB
/
Copy pathlinear-gpu-ref-ops.h
File metadata and controls
174 lines (135 loc) · 4.22 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// tensor/linear-gpu-ref-ops.h
// Copyright 2019 Johns Hopkins University (author: Daniel Povey)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#ifndef KALDI_TENSOR_LINEAR_REF_OPS_H_
#define KALDI_TENSOR_LINEAR_REF_OPS_H_ 1
#include "tensor/tensor.h"
#include "tensor/op.h"
#include "tensor/linear-ops.h"
#include "tensor/linear-special-ops.h"
// This header contains the "reference version" of linear Ops;
// this is the very simple, not-efficient version that runs on
// CPU when we run in "reference mode" (or when we encounter
// some combination that can't be run using our normal BLAS-based
// speciailized Ops).
namespace kaldi {
namespace tensor {
// Corresponds to the command a += b.
template <typename T>
class PlusEqRefOp: public Op {
PlusEqRefOp(const Tensor &a, const Tensor &b):
a_(a), b_(b) {
KALDI_ASSERT(!Overlap(a, b) && BroadcastableAndCompatible(a, b));
}
int32 Properties() { return kConcreteOp ; }
Op *Copy() const override {
return new PlusEqRefOp<T>(a_, b_);
}
void Do() const override {
RecordUse(a_, kReadWrite);
RecordUse(b_, kRead);
Do(a_.GetData<T>(), b_.GetData<T>,
KALDI_TENSOR_MAX_DIM - 1);
}
private:
void Do(T *a, const T *b, int32 raxis) {
int32 dim = std::max<int32>(a_.dims[raxis], b_.dims[raxis]),
a_stride = a_.strides[raxis], b_stride = b_.strides[raxis];
if (raxis == 0) {
for (int32 i = 0; i < dim; i++) {
a[i * a_stride] += b[i * b_stride];
}
} else {
for (int32 i = 0; i < dim; i++) {
Do(a + i * a_stride, b + i * b_stride, raxis - 1);
}
}
}
Tensor a_;
Tensor b_;
};
template <typename T>
class SetZeroRefOp: public Op {
SetZeroRefOp(const Tensor &a):
a_(a) { }
int32 Properties() { return kConcreteOp ; }
Op *Copy() const override {
return new SetZeroRefOp<T>(a_);
}
void Do() const override {
RecordUse(a_, kWrite);
Do(a_.GetData<T>(), KALDI_TENSOR_MAX_DIM - 1);
}
private:
void Do(T *a, int32 raxis) {
int32 dim = a_.dims[raxis],
stride = a_.strides[raxis];
if (raxis == 0) {
for (int32 i = 0; i < dim; i++) {
a[i * a_stride] = 0;
}
} else {
for (int32 i = 0; i < dim; i++) {
Do(a + i * a_stride, raxis - 1);
}
}
}
Tensor a_;
};
// T is the data-type of a, U is the data-type of b;
// this Op supports type conversion.
template <typename T, typename U>
class AssignRefOp: public Op {
PlusEqRefOp(const Tensor &a, const Tensor &b):
a_(a), b_(b) {
// The DimsGeq() makes sure there is no summation, as this version of the op
// does not support summation.
KALDI_ASSERT(!Overlap(a, b) && Compatible(a, b) &&
Broadcastable(a, b) &&
DimsGeq(a.Pattern(), b.Pattern()));
}
int32 Properties() { return kConcreteOp ; }
Op *Copy() const override {
return new PlusEqRefOp<T>(a_, b_);
}
void Do() const override {
RecordUse(a_, kWrite);
RecordUse(b_, kRead);
Do(a_.GetData<T>(), b_.GetData<U>,
KALDI_TENSOR_MAX_DIM - 1);
}
private:
void Do(T *a, const U *b, int32 raxis) {
int32 dim = std::max<int32>(a_.dims[raxis], b_.dims[raxis]),
a_stride = a_.strides[raxis], b_stride = b_.strides[raxis];
if (raxis == 0) {
for (int32 i = 0; i < dim; i++) {
a[i * a_stride] = static_cast<T>(b[i * b_stride]);
}
} else {
for (int32 i = 0; i < dim; i++) {
Do(a + i * a_stride, b + i * b_stride, raxis - 1);
}
}
}
Tensor a_;
Tensor b_;
};
}
}
} // namespace tensor
} // namespace kaldi
#endif // KALDI_TENSOR_LINEAR_REF_OPS_H_