forked from facebookresearch/FBTT-Embedding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt_embeddings.cpp
161 lines (144 loc) · 4.18 KB
/
tt_embeddings.cpp
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
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
#include <ATen/ATen.h>
#include <torch/extension.h>
using namespace at;
Tensor tt_embeddings_forward_cuda(
int32_t batch_count,
int32_t num_tables,
int32_t B,
int32_t D,
const std::vector<int>& tt_p_shapes,
const std::vector<int>& tt_q_shapes,
const std::vector<int>& tt_ranks,
Tensor L,
int32_t nnz,
Tensor indices,
Tensor rowidx,
Tensor tableidx,
const std::vector<Tensor>& tt_cores);
std::vector<Tensor> tt_embeddings_backward_dense_cuda(
int32_t batch_count,
int32_t D,
const std::vector<int32_t>& tt_p_shapes,
const std::vector<int32_t>& tt_q_shapes,
const std::vector<int32_t>& tt_ranks,
Tensor L,
int32_t nnz,
Tensor indices,
Tensor offsets,
Tensor tableidx,
Tensor d_output,
std::vector<Tensor>& tt_cores);
void tt_embeddings_backward_sgd_cuda(
int32_t batch_count,
int32_t D,
float learning_rate,
const std::vector<int32_t>& tt_p_shapes,
const std::vector<int32_t>& tt_q_shapes,
const std::vector<int32_t>& tt_ranks,
Tensor L,
int32_t nnz,
Tensor indices,
Tensor offsets,
Tensor tableidx,
Tensor d_output,
std::vector<Tensor>& tt_cores);
void tt_embeddings_backward_adagrad_cuda(
int32_t batch_count,
int32_t D,
float learning_rate,
float eps,
const std::vector<int32_t>& tt_p_shapes,
const std::vector<int32_t>& tt_q_shapes,
const std::vector<int32_t>& tt_ranks,
Tensor L,
int32_t nnz,
Tensor indices,
Tensor offsets,
Tensor tableidx,
Tensor d_output,
std::vector<Tensor>& optimizer_state,
std::vector<Tensor>& tt_cores);
void update_cache_state_cuda(Tensor indices, Tensor hashtbl, Tensor cache_freq);
void cache_populate_cuda(
int64_t num_embeddings,
const std::vector<int>& tt_p_shapes,
const std::vector<int>& tt_q_shapes,
const std::vector<int>& tt_ranks,
const std::vector<Tensor>& tt_cores,
Tensor L,
Tensor hashtbl,
Tensor cache_freq,
Tensor cache_state,
Tensor cache_weight);
std::tuple<Tensor, Tensor, Tensor, int32_t, c10::optional<Tensor>>
preprocess_indices_sync_cuda(
Tensor colidx,
Tensor offsets,
int32_t num_tables,
bool warmup,
Tensor hashtbl,
Tensor cache_state);
void cache_forward_cuda(
int32_t B,
int32_t nnz,
Tensor cache_locations,
Tensor rowidx,
Tensor cache_weight,
Tensor output);
void cache_backward_sgd_cuda(
int32_t nnz,
Tensor grad_output,
Tensor cache_locations,
Tensor rowidx,
float learning_rate,
Tensor cache_weight);
Tensor cache_backward_dense_cuda(
int32_t nnz,
Tensor grad_output,
Tensor cache_locations,
Tensor rowidx,
float learning_rate,
Tensor cache_weight);
void cache_backward_rowwise_adagrad_approx_cuda(
int32_t nnz,
Tensor grad_output,
Tensor cache_locations,
Tensor rowidx,
float learning_rate,
float eps,
Tensor cache_optimizer_state,
Tensor cache_weight);
PYBIND11_MODULE(tt_embeddings, m) {
m.def("tt_forward", &tt_embeddings_forward_cuda, "tt_forward()");
m.def(
"tt_dense_backward",
&tt_embeddings_backward_dense_cuda,
"tt_dense_backward()");
m.def(
"tt_sgd_backward", &tt_embeddings_backward_sgd_cuda, "tt_sgd_backward()");
m.def(
"tt_adagrad_backward",
&tt_embeddings_backward_adagrad_cuda,
"tt_adagrad_backward()");
m.def("update_cache_state", &update_cache_state_cuda, "update_cache_state()");
m.def("cache_populate", &cache_populate_cuda, "cache_populate()");
m.def(
"preprocess_indices_sync",
&preprocess_indices_sync_cuda,
"preprocess_colidx_sync()");
m.def("cache_forward", &cache_forward_cuda, "cache_forward()");
m.def("cache_backward_sgd", &cache_backward_sgd_cuda, "cache_backward_sgd()");
m.def(
"cache_backward_dense",
&cache_backward_dense_cuda,
"cache_backward_dense()");
m.def(
"cache_backward_rowwise_adagrad_approx",
&cache_backward_rowwise_adagrad_approx_cuda,
"cache_backward_rowwise_adagrad_approx()");
}