gpu_buf.h 2 KB
Newer Older
Li Zhang's avatar
Li Zhang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (c) 2019-2021, NVIDIA CORPORATION.  All rights reserved.
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "cuda_fp16.h"
lvhan028's avatar
lvhan028 committed
20
21
#include "src/turbomind/utils/cuda_fp8_utils.h"
#include "src/turbomind/utils/memory_utils.h"
Li Zhang's avatar
Li Zhang committed
22
23
24
25
26
27

#include <cstdlib>
#include <stdexcept>
#include <type_traits>
#include <vector>

lvhan028's avatar
lvhan028 committed
28
namespace turbomind {
Li Zhang's avatar
Li Zhang committed
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

template<typename T>
class GPUBuf {
public:
    GPUBuf(size_t size, bool random_init = true): size(size), ptr(nullptr)
    {
        deviceMalloc(&ptr, size, random_init);
    }
    template<typename T2>
    GPUBuf(const GPUBuf<T2>& buf_src): size(buf_src.size), ptr(nullptr)
    {
        deviceMalloc(&ptr, size, false);
        set(buf_src);
    }

    template<typename T2>
    void set(const GPUBuf<T2>& buf_src)
    {
        if (std::is_same<T, T2>::value) {
            cudaD2Dcpy(ptr, reinterpret_cast<T*>(buf_src.ptr), size);
        }
        else {
            invokeCudaCast(ptr, buf_src.ptr, size, 0);
        }
    }

    void set(const T* h_ptr)
    {
        cudaH2Dcpy(ptr, h_ptr, size);
    }

    void to_host(T* h_ptr) const
    {
        cudaD2Hcpy(h_ptr, ptr, size);
    }

    std::vector<T> to_host_vec() const
    {
        std::vector<T> host_vec(size);
        cudaD2Hcpy(host_vec.data(), ptr, size);
        return host_vec;
    }

    void zero()
    {
        deviceMemSetZero(ptr, size);
    }

    ~GPUBuf()
    {
        if (ptr != nullptr)
            cudaFree(ptr);
    }

    size_t size;
    T*     ptr;
};

lvhan028's avatar
lvhan028 committed
87
}  // namespace turbomind