binding.cpp 4.45 KB
Newer Older
chenxl's avatar
chenxl committed
1
/**
2
 * @Description  :
3
 * @Author       : Azure-Tang, Boxin Zhang
chenxl's avatar
chenxl committed
4
 * @Date         : 2024-07-25 13:38:30
5
 * @Version      : 0.2.2
6
 * @Copyright (c) 2024 by KVCache.AI, All Rights Reserved.
chenxl's avatar
chenxl committed
7
8
9
**/

#include "custom_gguf/ops.h"
Xiaodong Ye's avatar
Xiaodong Ye committed
10
#ifdef KTRANSFORMERS_USE_CUDA
chenxl's avatar
chenxl committed
11
#include "gptq_marlin/ops.h"
Xiaodong Ye's avatar
Xiaodong Ye committed
12
#endif
chenxl's avatar
chenxl committed
13
14
15
16
17
18
19
20
21
// Python bindings
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <torch/library.h>
#include <torch/extension.h>
#include <torch/torch.h>
// namespace py = pybind11;

PYBIND11_MODULE(KTransformersOps, m) {
22

23
24
25
    m.def("dequantize_q8_0", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q8_0((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
26
27
28
        }, "Function to dequantize q8_0 data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

29
30
31
    m.def("dequantize_q6_k", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q6_k((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
32
33
34
        }, "Function to dequantize q6_k data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

35
36
37
    m.def("dequantize_q5_k", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q5_k((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
38
39
40
        }, "Function to dequantize q5_k data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

41
42
43
    m.def("dequantize_q4_k", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q4_k((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
44
45
46
        }, "Function to dequantize q4_k data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

47
48
49
    m.def("dequantize_q3_k", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q3_k((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
50
51
52
        }, "Function to dequantize q3_k data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

53
54
55
    m.def("dequantize_q2_k", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_q2_k((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
56
57
58
        }, "Function to dequantize q2_k data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));

59
60
61
    m.def("dequantize_iq4_xs", [](const intptr_t data, int num_bytes, int blk_size, const int ele_per_blk, torch::Device device, py::object target_dtype) {
        torch::Dtype dtype = torch::python::detail::py_object_to_dtype(target_dtype);
        return dequantize_iq4_xs((int8_t*)data, num_bytes, blk_size, ele_per_blk, device, dtype);
62
63
        }, "Function to dequantize iq4_xs data.",
        py::arg("data"), py::arg("num_bytes"), py::arg("blk_size"), py::arg("ele_per_blk"), py::arg("device"), py::arg("target_dtype"));
64

Xiaodong Ye's avatar
Xiaodong Ye committed
65
#ifdef KTRANSFORMERS_USE_CUDA
66
67
68
69
    m.def("gptq_marlin_gemm", &gptq_marlin_gemm, "Function to perform GEMM using Marlin quantization.",
        py::arg("a"), py::arg("b_q_weight"), py::arg("b_scales"), py::arg("g_idx"),
        py::arg("perm"), py::arg("workspace"), py::arg("num_bits"), py::arg("size_m"),
        py::arg("size_n"), py::arg("size_k"), py::arg("is_k_full"));
70
#endif
71
}