Gaudi
GaudiLLM #
Bases: HuggingFaceLLM
GaudiLLM LLM.
Examples:
pip install llama-index-llms-gaudi
from llama_index.llms.gaudi import GaudiLLM
import argparse
import os, logging
def setup_parser(parser):
# Arguments management
parser.add_argument(
"--device", "-d", type=str, choices=["hpu"], help="Device to run", default="hpu"
)
parser.add_argument(
"--model_name_or_path",
default=None,
type=str,
# required=True,
help="Path to pre-trained model (on the HF Hub or locally).",
)
parser.add_argument(
"--bf16",
default=True,
action="store_true",
help="Whether to perform generation in bf16 precision.",
)
parser.add_argument(
"--max_new_tokens", type=int, default=100, help="Number of tokens to generate."
)
parser.add_argument(
"--max_input_tokens",
type=int,
default=0,
help="If > 0 then pad and truncate the input sequences to this specified length of tokens. \
if == 0, then truncate to 16 (original default) \
if < 0, then do not truncate, use full input prompt",
)
parser.add_argument("--batch_size", type=int, default=1, help="Input batch size.")
parser.add_argument(
"--warmup",
type=int,
default=3,
help="Number of warmup iterations for benchmarking.",
)
parser.add_argument(
"--n_iterations",
type=int,
default=5,
help="Number of inference iterations for benchmarking.",
)
parser.add_argument(
"--local_rank", type=int, default=0, metavar="N", help="Local process rank."
)
parser.add_argument(
"--use_kv_cache",
default=True,
action="store_true",
help="Whether to use the key/value cache for decoding. It should speed up generation.",
)
parser.add_argument(
"--use_hpu_graphs",
default=True,
action="store_true",
help="Whether to use HPU graphs or not. Using HPU graphs should give better latencies.",
)
parser.add_argument(
"--dataset_name",
default=None,
type=str,
help="Optional argument if you want to assess your model on a given dataset of the HF Hub.",
)
parser.add_argument(
"--column_name",
default=None,
type=str,
help="If `--dataset_name` was given, this will be the name of the column to use as prompts for generation.",
)
parser.add_argument(
"--do_sample",
action="store_true",
help="Whether to use sampling for generation.",
)
parser.add_argument(
"--num_beams",
default=1,
type=int,
help="Number of beams used for beam search generation. 1 means greedy search will be performed.",
)
parser.add_argument(
"--trim_logits",
action="store_true",
help="Calculate logits only for the last token to save memory in the first step.",
)
parser.add_argument(
"--seed",
default=27,
type=int,
help="Seed to use for random generation. Useful to reproduce your runs with `--do_sample`.",
)
parser.add_argument(
"--profiling_warmup_steps",
default=0,
type=int,
help="Number of steps to ignore for profiling.",
)
parser.add_argument(
"--profiling_steps",
default=0,
type=int,
help="Number of steps to capture for profiling.",
)
parser.add_argument(
"--profiling_record_shapes",
default=False,
type=bool,
help="Record shapes when enabling profiling.",
)
parser.add_argument(
"--prompt",
default=None,
type=str,
nargs="*",
help='Optional argument to give a prompt of your choice as input. Can be a single string (eg: --prompt "Hello world"), or a list of space-separated strings (eg: --prompt "Hello world" "How are you?")',
)
parser.add_argument(
"--bad_words",
default=None,
type=str,
nargs="+",
help="Optional argument list of words that are not allowed to be generated.",
)
parser.add_argument(
"--force_words",
default=None,
type=str,
nargs="+",
help="Optional argument list of words that must be generated.",
)
parser.add_argument(
"--assistant_model",
default=None,
type=str,
help="Optional argument to give a path to a draft/assistant model for assisted decoding.",
)
parser.add_argument(
"--peft_model",
default=None,
type=str,
help="Optional argument to give a path to a PEFT model.",
)
parser.add_argument("--num_return_sequences", type=int, default=1)
parser.add_argument(
"--token",
default=None,
type=str,
help="The token to use as HTTP bearer authorization for remote files. If not specified, will use the token "
"generated when running `huggingface-cli login` (stored in `~/.huggingface`).",
)
parser.add_argument(
"--model_revision",
default="main",
type=str,
help="The specific model version to use (can be a branch name, tag name or commit id).",
)
parser.add_argument(
"--attn_softmax_bf16",
action="store_true",
help="Whether to run attention softmax layer in lower precision provided that the model supports it and "
"is also running in lower precision.",
)
parser.add_argument(
"--output_dir",
default=None,
type=str,
help="Output directory to store results in.",
)
parser.add_argument(
"--bucket_size",
default=-1,
type=int,
help="Bucket size to maintain static shapes. If this number is negative (default is -1) \
then we use `shape = prompt_length + max_new_tokens`. If a positive number is passed \
we increase the bucket in steps of `bucket_size` instead of allocating to max (`prompt_length + max_new_tokens`).",
)
parser.add_argument(
"--bucket_internal",
action="store_true",
help="Split kv sequence into buckets in decode phase. It improves throughput when max_new_tokens is large.",
)
parser.add_argument(
"--dataset_max_samples",
default=-1,
type=int,
help="If a negative number is passed (default = -1) perform inference on the whole dataset, else use only `dataset_max_samples` samples.",
)
parser.add_argument(
"--limit_hpu_graphs",
action="store_true",
help="Skip HPU Graph usage for first token to save memory",
)
parser.add_argument(
"--reuse_cache",
action="store_true",
help="Whether to reuse key/value cache for decoding. It should save memory.",
)
parser.add_argument(
"--verbose_workers",
action="store_true",
help="Enable output from non-master workers",
)
parser.add_argument(
"--simulate_dyn_prompt",
default=None,
type=int,
nargs="*",
help="If empty, static prompt is used. If a comma separated list of integers is passed, we warmup and use those shapes for prompt length.",
)
parser.add_argument(
"--reduce_recompile",
action="store_true",
help="Preprocess on cpu, and some other optimizations. Useful to prevent recompilations when using dynamic prompts (simulate_dyn_prompt)",
)
parser.add_argument(
"--use_flash_attention",
action="store_true",
help="Whether to enable Habana Flash Attention, provided that the model supports it.",
)
parser.add_argument(
"--flash_attention_recompute",
action="store_true",
help="Whether to enable Habana Flash Attention in recompute mode on first token generation. This gives an opportunity of splitting graph internally which helps reduce memory consumption.",
)
parser.add_argument(
"--flash_attention_causal_mask",
action="store_true",
help="Whether to enable Habana Flash Attention in causal mode on first token generation.",
)
parser.add_argument(
"--flash_attention_fast_softmax",
action="store_true",
help="Whether to enable Habana Flash Attention in fast softmax mode.",
)
parser.add_argument(
"--book_source",
action="store_true",
help="Whether to use project Guttenberg books data as input. Useful for testing large sequence lengths.",
)
parser.add_argument(
"--torch_compile",
action="store_true",
help="Whether to use torch compiled model or not.",
)
parser.add_argument(
"--ignore_eos",
default=True,
action=argparse.BooleanOptionalAction,
help="Whether to ignore eos, set False to disable it",
)
parser.add_argument(
"--temperature",
default=1.0,
type=float,
help="Temperature value for text generation",
)
parser.add_argument(
"--top_p",
default=1.0,
type=float,
help="Top_p value for generating text via sampling",
)
parser.add_argument(
"--const_serialization_path",
"--csp",
type=str,
help="Path to serialize const params. Const params will be held on disk memory instead of being allocated on host memory.",
)
parser.add_argument(
"--disk_offload",
action="store_true",
help="Whether to enable device map auto. In case no space left on cpu, weights will be offloaded to disk.",
)
parser.add_argument(
"--trust_remote_code",
action="store_true",
help="Whether or not to allow for custom models defined on the Hub in their own modeling files.",
)
args = parser.parse_args()
if args.torch_compile:
args.use_hpu_graphs = False
if not args.use_hpu_graphs:
args.limit_hpu_graphs = False
args.quant_config = os.getenv("QUANT_CONFIG", "")
if args.quant_config == "" and args.disk_offload:
logger.warning(
"`--disk_offload` was tested only with fp8, it may not work with full precision. If error raises try to remove the --disk_offload flag."
)
return args
def messages_to_prompt(messages):
prompt = ""
for message in messages:
if message.role == 'system':
prompt += f"<|system|>\n{message.content}</s>\n"
elif message.role == 'user':
prompt += f"<|user|>\n{message.content}</s>\n"
elif message.role == 'assistant':
prompt += f"<|assistant|>\n{message.content}</s>\n"
# ensure we start with a system prompt, insert blank if needed
if not prompt.startswith("<|system|>\n"):
prompt = "<|system|>\n</s>\n" + prompt
# add final assistant prompt
prompt = prompt + "<|assistant|>\n"
return prompt
def completion_to_prompt(completion):
return f"<|system|>\n</s>\n<|user|>\n{completion}</s>\n<|assistant|>\n"
import torch
from llama_index.core.prompts import PromptTemplate
from llama_index.llms.optimum-intel import GaudiLLM
parser = argparse.ArgumentParser(description="GaudiLLM Basic Usage Example")
args = setup_parser(parser)
args.model_name_or_path = "HuggingFaceH4/zephyr-7b-alpha"
llm = GaudiLLM(
args=args,
logger=logger,
model_name="HuggingFaceH4/zephyr-7b-alpha",
tokenizer_name="HuggingFaceH4/zephyr-7b-alpha",
query_wrapper_prompt=PromptTemplate(
"<|system|>\n</s>\n<|user|>\n{query_str}</s>\n<|assistant|>\n"
),
context_window=3900,
max_new_tokens=256,
generate_kwargs={"temperature": 0.7, "top_k": 50, "top_p": 0.95},
messages_to_prompt=messages_to_prompt,
device_map="auto",
)
response = llm.complete("What is the meaning of life?")
print(str(response))
Source code in llama-index-integrations/llms/llama-index-llms-gaudi/llama_index/llms/gaudi/base.py
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|