INSTRUCTLAB.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. == Testing InstructLab Models Locally ==
  2. === What is InstructLab? ===
  3. There's a large variety of https://huggingface.co/models[models] available from https://huggingface.co[HuggingFace], and https://huggingface.co/instructlab[InstructLab] is an open-source collection of LLMs with tools that allow users to both use, and improve, LLMs based on Granite models.
  4. There are also model container images available on https://catalog.redhat.com/search?gs&q=granite%208b[Red Hat Ecosystem Catalog] (the link is just for the Granite 8b family).
  5. A https://developers.redhat.com/articles/2024/08/01/open-source-ai-coding-assistance-granite-models[Red Hat blog] by Cedric Clyburn shows how you can use Ollama and InstructLab to run LLMs locally in a lot more detail, so I'll keep it short and with a focus on Conda here.
  6. === Setting Up the Environment ===
  7. You can use one of the provided environment files, `env-ilab-25.yml`, to create a Conda environment with the `instructlab` package version `0.25.x`.
  8. This gives you the basic environment that enables you to start serving and chatting to various HuggingFace (and other) Transformer-based models.
  9. Just like with any other Conda environment, start by creating the desired configuration.
  10. [subs="+quotes"]
  11. ----
  12. $ *source conda-init.sh*
  13. (base) $ *mamba env create -y -f envs/env-ilab-25.yml*
  14. Channels:
  15. - conda-forge
  16. Platform: osx-arm64
  17. Collecting package metadata (repodata.json): done
  18. Solving environment: done
  19. Downloading and Extracting Packages:
  20. ...
  21. Preparing transaction: done
  22. Verifying transaction: done
  23. Executing transaction: done
  24. ...
  25. ----
  26. ====
  27. NOTE: The installation uses `pip` to install `instructlab` as there are no Conda Forge packages for it. Be patient, it takes quite some time.
  28. ====
  29. Activate the environment and create a `bash` completion file.
  30. [subs="+quotes"]
  31. ----
  32. (base) $ *mamba activate ilab-25*
  33. (ilab-25) $ *_ILAB_COMPLETE=bash_source ilab > ilab.completion*
  34. (ilab-25) $ *source ilab.completion*
  35. ----
  36. Check the system information.
  37. [subs="+quotes"]
  38. ----
  39. (ilab-25) $ *ilab system info*
  40. Platform:
  41. sys.version: 3.11.12 | packaged by conda-forge | (main, Apr 10 2025, 22:18:52) [Clang 18.1.8 ]
  42. sys.platform: darwin
  43. os.name: posix
  44. platform.release: 24.4.0
  45. platform.machine: arm64
  46. platform.node: foobar
  47. platform.python_version: 3.11.12
  48. platform.cpu_brand: Apple M1 Max
  49. memory.total: 64.00 GB
  50. memory.available: 25.36 GB
  51. memory.used: 14.97 GB
  52. InstructLab:
  53. instructlab.version: 0.25.0
  54. ...
  55. Torch:
  56. torch.version: 2.5.1
  57. ...
  58. __torch.backends.mps.is_built: True
  59. torch.backends.mps.is_available: True__
  60. llama_cpp_python:
  61. llama_cpp_python.version: 0.3.6
  62. _llama_cpp_python.supports_gpu_offload: True_
  63. ----
  64. The PyTorch `mps` and Llama `supports_gpu_offload` settings show that InstructLab is capable of using the M1 Max GPU for serving.
  65. === Downloading Models ===
  66. Visit the InstructLab page and choose a model to download (for this demo, I selected `granite-3.0-8b-lab-community`).
  67. Use the `ilab model download` command to pull it.
  68. By default, models will be stored in `~/.cache/instructlab/models/`, unless you say otherwise with the `--model-dir` option to `ilab model` command.
  69. [subs="+quotes"]
  70. ----
  71. (ilab-25) $ *ilab model download -rp instructlab/granite-3.0-8b-lab-community*
  72. INFO 2025-04-14 13:29:59,724 instructlab.model.download:77: Downloading model from Hugging Face:
  73. Model: instructlab/granite-3.0-8b-lab-community@main
  74. Destination: /foo/bar/.cache/instructlab/models
  75. ...
  76. INFO 2025-04-14 13:36:13,171 instructlab.model.download:288:
  77. ᕦ(òᴗóˇ)ᕤ instructlab/granite-3.0-8b-lab-community model download completed successfully! ᕦ(òᴗóˇ)ᕤ
  78. INFO 2025-04-14 13:36:13,171 instructlab.model.download:302: Available models (\`ilab model list`):
  79. +------------------------------------------+...+---------+--------------------------+
  80. | Model Name |...| Size | Absolute path |
  81. +------------------------------------------+...+---------+--------------------------+
  82. | instructlab/granite-3.0-8b-lab-community |...| 15.2 GB | .../models/instructlab |
  83. +------------------------------------------+...+---------+--------------------------+
  84. ----
  85. ====
  86. NOTE: LLMs are usually quite large (as the name suggests) so be patient and set aside sufficient amount of disk space. The above model is a total download of 17 GiB, so even on a fast link it takes a couple of minutes to download.
  87. ====
  88. Note that the absolute path to model is a directory - if you look inside it, there will be a subdirectory containing the actual download.
  89. The format of the model is HuggingFace _safetensors_, which requires the https://github.com/vllm-project/vllm.git[vLLM] serving backend, and is not supported on macOS by default.
  90. From here on, there are two options: either install vLLM manually, or use `llama.cpp` to convert the model to GGUF.
  91. === Installing vLLM on macOS ===
  92. If you used the InstructLab env file provided in this repo, you should already have `torch` and `torchvision` modules in the environment. If not, ensure they are available.
  93. First, clone Triton and install it.
  94. [subs="+quotes"]
  95. ----
  96. (ilab-25) $ *git clone https://github.com/triton-lang/triton.git*
  97. Cloning into 'triton'...
  98. ...
  99. (ilab-25) $ *cd triton/python*
  100. (ilab-25) $ *pip install cmake*
  101. Collecting cmake
  102. ...
  103. Successfully installed cmake-4.0.0
  104. (ilab-25) $ *pip install -e .*
  105. Obtaining file:///foo/bar/baz/triton/python
  106. ...
  107. Successfully built triton
  108. Installing collected packages: triton
  109. Successfully installed triton-3.3.0+git32b42821
  110. (ilab-25) $ *cd ../..*
  111. (ilab-25) $ *rm -rf ./triton/*
  112. ----
  113. Clone vLLM and build it.
  114. [subs="+quotes"]
  115. ----
  116. (ilab-25) $ *git clone https://github.com/vllm-project/vllm.git*
  117. Cloning into 'vllm'...
  118. ...
  119. (ilab-25) $ *cd vllm*
  120. (ilab-25) $ *sed -i 's/^triton==3.2/triton==3.3/' requirements/requirements-cpu.txt
  121. (ilab-25) $ *pip install -e .*
  122. Obtaining file:///foo/bar/baz/vllm
  123. ...
  124. Successfully built vllm
  125. Installing collected packages: vllm
  126. Successfully installed vllm-0.8.5.dev3+g7cbfc1094.d20250414
  127. (ilab-25) $ *cd ..*
  128. (ilab-25) $ *rm -rf ./vllm/*
  129. ----
  130. === Converting Models to GGUF ===
  131. You can use https://github.com/ggerganov/llama.cpp.git[`llama.cpp`] to convert models from HF, GGML, and LORA model formats to GGUF, which InstructLab can serve even on a Mac.
  132. Clone and build `llama.cpp`.
  133. [subs="+quotes"]
  134. ----
  135. (ilab-25) $ *git clone https://github.com/ggerganov/llama.cpp.git*
  136. Cloning into 'llama.cpp'...
  137. ...
  138. (ilab-25) $ *cd llama.cpp*
  139. (ilab-25) $ *pip install --upgrade -r requirements.txt*
  140. Looking in indexes: https://pypi.org/simple, ...
  141. ...
  142. Successfully installed aiohttp-3.9.5 ...
  143. ----
  144. You can now use the various `convert_*.py` scripts. In our case, it would be HF (HuggingFace) to GGUF conversion.
  145. [subs="+quotes"]
  146. ----
  147. (ilab-25) $ *./convert_hf_to_gguf.py \*
  148. *~/.cache/instructlab/models/instructlab/granite-3.0-8b-lab-community/ \*
  149. *--outfile ~/.cache/instructlab/models/granite-3.0-8b-lab-community.gguf \*
  150. *--outtype q8_0*
  151. INFO:hf-to-gguf:Loading model: granite-3.0-8b-lab-community
  152. INFO:gguf.gguf_writer:gguf: This GGUF file is for Little Endian only
  153. INFO:hf-to-gguf:Exporting model...
  154. INFO:hf-to-gguf:gguf: loading model weight map from 'model.safetensors.index.json'
  155. INFO:hf-to-gguf:gguf: loading model part 'model-00001-of-00004.safetensors'
  156. ...
  157. INFO:hf-to-gguf:Model successfully exported to /foo/bar/.cache/instructlab/models/granite-3.0-8b-lab-community.gguf
  158. (ilab-25) $ ilab model list
  159. +------------------------------------------+...+---------+---------------------------------------+
  160. | Model Name |...| Size | Absolute path |
  161. +------------------------------------------+...+---------+---------------------------------------+
  162. | instructlab/granite-3.0-8b-lab-community |...| 15.2 GB | .../instructlab |
  163. | granite-3.0-8b-lab-community.gguf |...| 8.1 GB | .../granite-3.0-8b-lab-community.gguf |
  164. +------------------------------------------+...+---------+---------------------------------------+
  165. ----
  166. Reference: https://github.com/ggml-org/llama.cpp/discussions/2948[Tutorial: How to convert HuggingFace model to GGUF format] on GitHub.
  167. === Serving Models ===
  168. Start the model server.
  169. [subs="+quotes"]
  170. ----
  171. (ilab-25) $ *ilab model serve \*
  172. *--model-path /foo/bar/.cache/instructlab/models/granite-3.0-8b-lab-community.gguf*
  173. INFO 2025-04-14 14:49:05,624 instructlab.model.serve_backend:79: Setting backend_type in the serve config to llama-cpp
  174. INFO 2025-04-14 14:49:05,633 instructlab.model.serve_backend:85: Using model '/foo/bar/.cache/instructlab/models/granite-3.0-8b-lab-community.gguf' with -1 gpu-layers and 4096 max context size.
  175. ...
  176. INFO 2025-04-14 14:49:12,050 instructlab.model.backends.llama_cpp:233: Starting server process, press CTRL+C to shutdown server...
  177. INFO 2025-04-14 14:49:12,050 instructlab.model.backends.llama_cpp:234: After application startup complete see http://127.0.0.1:8000/docs for API.
  178. ----
  179. In another terminal, start a chat.
  180. [subs="+quotes"]
  181. ----
  182. (ilab-25) $ *ilab model chat*
  183. ╭─────────────────────────────────────── system ────────────────────────────────────────╮
  184. │ Welcome to InstructLab Chat w/ GRANITE-3.0-8B-LAB-COMMUNITY.GGUF (type /h for help) │
  185. ╰───────────────────────────────────────────────────────────────────────────────────────╯
  186. >>> *what are your specialties?*
  187. My specialties include providing assistance with general tasks such as setting up a new device, troubleshooting software issues, and answering basic questions about using technology.
  188. I can also help with more specific tasks related to Linux, such as configuring network settings, managing users and groups, and installing software packages. I have experience working with various Linux distributions, including Red Hat Enterprise Linux, Fedora, Ubuntu, and Debian.
  189. Additionally, I am familiar with a wide range of programming languages, tools, and frameworks, including Python, Java, C++, Ruby on Rails, AngularJS, React, and Node.js.
  190. I hope this information is helpful! Let me know if you have any other questions.
  191. ----
  192. Congratulations!