Text Generation¶
Overview¶
This tutorial explains how to run inference with a Llama3-8b model using the RBLN SDK C/C++ Runtime API
.
The model is compiled using the RBLN SDK Python API
, and the resulting *.rbln
file is used for inference with the
RBLN SDK C/C++ Runtime API
. This approach combines the ease of model preparation in Python with the performance
benefits of C/C++ for inference.
The entire code used in this tutorial can be found in RBLN Model Zoo.
Setup & Installation¶
Before you begin, ensure that your system environment is properly configured and that all required packages are installed. This includes:
- System Requirements:
- Python: 3.9–3.12
- RBLN Driver
- Packages Requirements:
- Installation Command:
Note
Please note that rebel-compiler
requires an RBLN Portal account.
Compilation with RBLN Python API
¶
Compile the Model¶
Import the RBLNLlamaForCausalLM
class from optimum-rbln
and use from_pretrained()
to download
and compile the model. The compiled model is saved to disk using model.save_pretrained()
.
Generate Input Data¶
Tokenize input using AutoTokenizer
from the transformers
library.
The generated input binary file c_input_ids.bin
is saved to disk.
Note
This tutorial demonstrates how to use RBLN SDK C/C++ Runtime API
for Llama3-8b
.
This example focuses on C/C++ based inference, so the pre- and post-processing, i.e. tokenization, are handled by Python APIs.
Inference with RBLN SDK C/C++ Runtime API
¶
Prepare CMake Build Script¶
Define dependencies and linking for the example application.
Note
${YOUR_SAMPLE_PATH}
refers to the directory containing the CMake file and inference code.
Prepare Code for Inference¶
Several source files are used for inference, including:
-
llama_main.cc: Inference application for model loading and execution.
-
llama_class_example.hpp/cc: Inference wrapper and workflow management.
-
llama_tensor_example.hpp/cc: Inference wrapper using RBLN SDK C/C++ Runtime API, including:
-
Prefill and decode stage management of
Llama3-8b
-
Input/output buffer handling
-
Model execution flow control
-
-
llama_tensor_op_example.hpp: Tensor manipulation operations.
llama_main.cc
llama_class_example.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
llama_class_example.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
llama_tensor_example.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
llama_tensor_op_example.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
Build with CMake and Run the Executable¶
Create a build directory, run cmake, compile the code, and execute the binary. The execution generates a binary file containing the token ID sequence.
Generate Text from Output Data¶
Decode the output token ID sequence generated by the C/C++ executable into recognizable text using a Python script.
Example Output: