moe.cpp 4.67 KB
Newer Older
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <torch/extension.h>
#include <torch/torch.h>

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

// CUDA runtime                                                                                                           
#include <cuda_runtime.h>                                                                                                 
#include <cublas_v2.h>                                                                                                    
                                                                                                                            
// CUDA and CUBLAS functions                                                                                              
//#include <helper_functions.h>                                                                                             
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
14
#include <helper_cuda.h> 
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
15
16


Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
17
const int num_stream=512;
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
18
19
20
21
22
23
24
25
26
27
28
29

// std::vector<torch::Tensor> 
void moe_cuda_forward(
        torch::Tensor input, // [B x D_model]
        torch::Tensor gate,  // [B x N]
        torch::Tensor weight, // [N x D_model x D_ffn]
        torch::Tensor bias // [N x D_ffn]
        ) {
    const auto batch_size = input.size(0);
    const auto num_expert = gate.size(1);
    const auto d_model = weight.size(1);
    const auto d_ffn = weight.size(2);
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
30
    printf("b=%d, expert=%d, d_model=%d, d_ffn=%d\n", batch_size, num_expert, d_model, d_ffn);
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
31
    auto output = input.new_zeros({batch_size, num_expert, d_ffn});
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
32
    
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
33
34

    cublasHandle_t handle;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
35
36
    checkCudaErrors(cublasCreate(&handle));
    
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
37
38
    cudaStream_t stream[num_stream];
    for (size_t i=0; i<num_stream; ++i) {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
39
        checkCudaErrors(cudaStreamCreate(&stream[i]));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
40
    }
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
41

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
42
43
44
45
46
    cudaEvent_t start, stop;
    checkCudaErrors(cudaEventCreate(&start));
    checkCudaErrors(cudaEventCreate(&stop));
    // Record the start event
    checkCudaErrors(cudaEventRecord(start, NULL));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
47
48
49
50
51
    
    size_t s;
    for (size_t i=0; i<batch_size; ++i) {
        for (size_t j=0; j<num_expert; ++j) {
            s = (i * num_expert + j) % num_stream;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
52
53
            // printf("i=%d j=%d goes to stream %d\n", i, j, s);
            checkCudaErrors(cublasSetStream(handle, stream[s]));
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
54
55
56
            if (input.scalar_type() == torch::ScalarType::Float) {
                float alpha = 1.0;
                float beta = 0.0;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
57
                checkCudaErrors(cublasSgemm(handle, 
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
58
59
                    CUBLAS_OP_N, 
                    CUBLAS_OP_N,
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
60
61
62
                    1, // m
                    d_ffn, // n
                    d_model, // k
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
63
                    &alpha,
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
64
                    input[i].data_ptr<float>(),
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
65
                    1,
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
66
                    weight.index(gate[i][j]).data_ptr<float>(),
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
67
68
                    d_model,
                    &beta,
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
69
70
                    output[i][j].data_ptr<float>(),
                    1));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
71
            } else {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
72
                printf("only support float!!!\n");
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
73
74
75
            }
        }
    }
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
76
77
78
79
80
81
82
83
84
    // checkCudaErrors(cudaDeviceSynchronize());
    // Record the stop event
    checkCudaErrors(cudaEventRecord(stop, NULL));

    // Wait for the stop event to complete
    checkCudaErrors(cudaEventSynchronize(stop));

    float msecTotal = 0.0f;
    checkCudaErrors(cudaEventElapsedTime(&msecTotal, start, stop));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
85

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
86
87
88
89
90
91
92
93
94
95
96
    // Compute and print the performance
    float msecPerMatrixMul = msecTotal / batch_size / num_expert;
    double flopsPerMatrixMul = 2.0 * (double)d_model * (double)d_ffn;
    double gigaFlops = (flopsPerMatrixMul * 1.0e-9f) / (msecPerMatrixMul / 1000.0f);
        printf(
            "Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops\n",
            gigaFlops,
            msecPerMatrixMul,
            flopsPerMatrixMul);

    // std::cout << output << std::endl;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
97
    
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
98
    for (size_t i=0; i<num_stream; ++i) {
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
99
        checkCudaErrors(cudaStreamDestroy(stream[i]));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
100
    }
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
101
    checkCudaErrors(cublasDestroy(handle));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
102
103
104
105
106
107
108
109
110
111
112
113
}


// 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
114
    int device=2;
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
115
116
117
118
119
    torch::Tensor input = torch::randn({2048, 512}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
    torch::Tensor gate = torch::zeros({2048, 2}, torch::dtype(torch::kInt64).device(torch::kCUDA, device));
    torch::Tensor weight = torch::randn({2, 512, 2048}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
    torch::Tensor bias = torch::randn({2, 2048}, torch::dtype(torch::kFloat32).device(torch::kCUDA, device));
    checkCudaErrors(cudaSetDevice(device));
Jiezhong Qiu's avatar
can run  
Jiezhong Qiu committed
120
121
    moe_cuda_forward(input, gate, weight, bias);
}