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

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

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
8
9
// CUDA runtime
#include <cuda.h>                                                                                             
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
10
11
12
13
14
#include <cuda_runtime.h>                                                                                                 
#include <cublas_v2.h>                                                                                                    
                                                                                                                            
// CUDA and CUBLAS functions                                                                                              
//#include <helper_functions.h>                                                                                             
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
15
#include <helper_cuda.h> 
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
16
17


Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
18
std::vector<torch::Tensor> moe_cuda_forward(
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
19
20
        torch::Tensor input, // [B x D_model]
        torch::Tensor gate,  // [B x K]
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
21
        torch::Tensor weight // [N x D_ffn x D_model]
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
22
23
24
25
26
27
28
29
30
31
32
33
        ) {
    /*
        The bias term should have been merged into weight. Note the following fact that 
        Wx+b = [W b] [x]
                     [1]  
    */
    const auto batch_size = input.size(0);
    const auto top_k = gate.size(1);
    const auto num_expert = weight.size(0);
    const auto out_feat = weight.size(1);
    const auto in_feat = weight.size(2);
    
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
34
    printf("b=%d, expert=%d, in_feat (d_model)=%d, out_feat (d_ffn)=%d, topk=%d\n", batch_size, num_expert, in_feat, out_feat, top_k);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
35
36
    auto output = input.new_zeros({batch_size, top_k, out_feat});

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
37
    AT_DISPATCH_FLOATING_TYPES(input.scalar_type(), "moe_cuda_forward", ([&] {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
38
39
40
41
42
43
44
45
46
47
48
        moe_cuda_forward_impl<scalar_t>(
            input.data_ptr<scalar_t>(),
            gate.data_ptr<size_t>(),
            weight.data_ptr<scalar_t>(),
            output.data_ptr<scalar_t>(),
            batch_size,
            top_k,
            in_feat,
            out_feat
        );
    }));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
49
    return {output, };
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
50
51
52
53
54
55
56
57
58
59
60
}

// 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)


int main() {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
61
    int device=2;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
62
    torch::Tensor input = torch::randn({2048, 512}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
63
    torch::Tensor gate = torch::zeros({2048, 2}, torch::dtype(torch::kInt64));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
64
65
    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
66
    moe_cuda_forward(input, gate, weight);
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
67
}