moe.cpp 3.39 KB
Newer Older
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
1
2
3
4
5
6
#include <torch/extension.h>

#include <cstdio>
#include <iostream>
#include <vector>

Rick Ho's avatar
Rick Ho committed
7
8
9
10
11
std::vector<torch::Tensor> moe_cuda_expert_count(
    torch::Tensor weight, // TODO: pass num-experts in another way?
    torch::Tensor gate);

std::vector<torch::Tensor> moe_cuda_local_scatter(
Jiezhong Qiu's avatar
updarte  
Jiezhong Qiu committed
12
    torch::Tensor input,
Rick Ho's avatar
Rick Ho committed
13
14
15
16
17
18
19
20
21
22
	torch::Tensor pos);

std::vector<torch::Tensor> moe_cuda_local_gather(
	torch::Tensor output_buf,
	torch::Tensor pos);

std::vector<torch::Tensor> moe_cuda_forward(
    torch::Tensor input_buf,
    torch::Tensor weight,
	torch::Tensor expert_count);
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
23

Jiezhong Qiu's avatar
Jiezhong Qiu committed
24
std::vector<torch::Tensor> moe_cuda_backward(
Rick Ho's avatar
Rick Ho committed
25
26
27
28
    torch::Tensor grad_output_buf,
    torch::Tensor input_buf,
    torch::Tensor weight,
	torch::Tensor expert_count);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
29

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
30
31
32
33
34
35
36
// C++ interface

// NOTE: AT_ASSERT has become AT_CHECK on master after 0.4.
#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor")
#define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous")
#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)

Rick Ho's avatar
Rick Ho committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
std::vector<torch::Tensor> moe_expert_count(
		torch::Tensor weight,
		torch::Tensor gate) {
	CHECK_INPUT(gate);
	return moe_cuda_expert_count(weight, gate);
}

std::vector<torch::Tensor> moe_local_scatter(
		torch::Tensor input,
		torch::Tensor pos) {
	CHECK_INPUT(input);
	return moe_cuda_local_scatter(input, pos);
}

std::vector<torch::Tensor> moe_local_gather(
		torch::Tensor output_buf,
		torch::Tensor pos) {
	CHECK_INPUT(output_buf);
	return moe_cuda_local_gather(output_buf, pos);
}


Jiezhong Qiu's avatar
Jiezhong Qiu committed
59
std::vector<torch::Tensor> moe_forward(
Rick Ho's avatar
Rick Ho committed
60
        torch::Tensor input_buf,     // [batch_size x in_feat]
Rick Ho's avatar
Rick Ho committed
61
        torch::Tensor weight,        // [num_expert x out_feat x in_feat]
Rick Ho's avatar
Rick Ho committed
62
        torch::Tensor expert_count   // [batch_size]
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
63
        ) {
Rick Ho's avatar
Rick Ho committed
64
    CHECK_INPUT(input_buf);
Rick Ho's avatar
Rick Ho committed
65
    CHECK_INPUT(weight);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
66
67
68
69
70
    /*
        The bias term should have been merged into weight. Note the following fact that 
        Wx+b = [W b] [x]
                     [1]  
    */
Rick Ho's avatar
Rick Ho committed
71
    return moe_cuda_forward(input_buf, weight, expert_count);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
72
73
}

Jiezhong Qiu's avatar
Jiezhong Qiu committed
74
std::vector<torch::Tensor> moe_backward(
Rick Ho's avatar
Rick Ho committed
75
76
77
78
        torch::Tensor grad_output_buf, // [batch_size x out_feat]
        torch::Tensor input_buf,       // [batch_size x out_feat]
        torch::Tensor weight,          // [num_expert x out_feat x in_feat]
        torch::Tensor expert_count
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
79
        ) {
Rick Ho's avatar
Rick Ho committed
80
81
    CHECK_INPUT(grad_output_buf);
    CHECK_INPUT(input_buf);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
82
    CHECK_INPUT(weight);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
83
84
85
86
87
    /*
        The bias term should have been merged into weight. Note the following fact that 
        Wx+b = [W b] [x]
                     [1]  
    */
Rick Ho's avatar
Rick Ho committed
88
    return moe_cuda_backward(grad_output_buf, input_buf, weight, expert_count);
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
89
90
91
}


Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
92
/*
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
93
int main() {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
94
    int device=2;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
95
    torch::Tensor input = torch::randn({2048, 512}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
96
    torch::Tensor gate = torch::zeros({2048, 2}, torch::dtype(torch::kInt64));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
97
98
    torch::Tensor weight = torch::randn({2, 512, 2048}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
    checkCudaErrors(cudaSetDevice(device));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
99
    moe_cuda_forward(input, gate, weight);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
100
101
102
103
}
*/

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
Rick Ho's avatar
Rick Ho committed
104
105
106
  m.def("expert_count", &moe_expert_count, "MoE expert count (CUDA)");
  m.def("local_scatter", &moe_local_scatter, "MoE local scatter (CUDA)");
  m.def("local_gather", &moe_local_gather, "MoE local gather (CUDA)");
Jiezhong Qiu's avatar
Jiezhong Qiu committed
107
108
  m.def("forward", &moe_forward, "MoE forward (CUDA)");
  m.def("backward", &moe_backward, "MoE backward (CUDA)");
Rick Ho's avatar
Rick Ho committed
109
}