Closed SunHaoOne closed 1 year ago
I’m sorry, I’m a beginner and I asked some basic questions. After my attempts, I simplified the following code and hope to help people who are having difficulties like me. Here is the minimal code:
xxx@xxx:~/Desktop/LightGBM/src/lleaves$ ls
c_bench.cpp c_bench.h CMakeLists.txt lleaves.o
#include "c_bench.h"
#include <vector>
#include <iostream>
int main()
{
std::vector<double> data = {8.81351540e+00, -2.74901880e-01, -4.78453119e-02, 2.25956985e+01,
-2.75495538e-01, -9.12007856e-02, -4.78453119e-02, 1.88485949e+00,
1.88485949e+00, 1.64226175e-03, 1.64226175e-03};
double result;
forest_root(data.data(), &result, 0, 1);
std::cout << "Result: " << result << std::endl;
return 0;
}
CMakeLists
cmake_minimum_required(VERSION 3.19)
project(c_bench)
set(CMAKE_CXX_STANDARD 11)
add_executable(c_bench c_bench.cpp)
add_custom_target(run ALL)
add_dependencies(c_bench run)
target_link_libraries(c_bench ${CMAKE_CURRENT_SOURCE_DIR}/lleaves.o)
cmake .. && make
./c_bench
g++ c_bench.cpp lleaves.o -o c_bench
./c_bench
Nice, I'm glad you got it working! I'll make sure to point others to your reference. If you'd like, you could add it to the docs and I'd add you as a contributor also.
Hello, thank you very much for your code, it has shown good performance in testing with Python. I'm sorry I wrote the issue in an existing question, then I reorganised my thoughts and tried some of your suggestions here. My idea is to compile the model trained in Python into an
ELF
binary file, and then the inferencecpp
code takes a line of data as input and outputs a number. When usingc_bench.cpp,
many libraries such as<cnpy.h>
,<benchmark/benchmark.h>
that are not currently needed are dependent, and due to network reasons, it is difficult to download the dataset, so the function has not been tested yet. (I will try to solve this dataset problem and try thec_bench example compile
later, But I may not have the ability to simplify this code, remove irrelevant header files, similar to the original LightGBM code mentioned in the 5th part below.).Here are the steps I have taken so far:
1. Generate the cache file by python
Then we will get a
lleaves.so
file here.2. Interface with cpp
There are the files in the directory:
In the
interface.cpp
, this is the main function to interface.Since I am not very familiar with the knowledge of this binary file, I am not sure if it is correct to modify
c_bench.cpp
in this way, so I keep the original entry functionbm_lleaves
.I am not sure how to add
extern C
to thec_bench.cpp
code. Here is one of my attempts.By the way, I did not modify the
c_bench.h
file. It already has extern C here.3. Compile with gcc
4. objdump check
After checking the
ELF
, I can find theforest_root
.5. Original LightGBM function
In the original LightGBM code, there are many dynamic libraries and header files that are dependent. However, in fact, not so many things are used during inference, so I consider using your code. My idea is to use the compiled model in your code for forward propagation, and remove irrelevant
cnpy
andbenchmark
header files. If possible, I hope you can provide a simple example.