ext-only Inference
For local inference we have provided an inference script. Depending on the type of finetuning performed during training the inference script takes different arguments.
To finetune all model parameters the output dir of the training has to be given as --model_name argument. In the case of a parameter efficient method like lora the base model has to be given as --model_name and the output dir of the training has to be given as --peft_model argument. Additionally, a prompt for the model in the form of a text file has to be provided. The prompt file can either be piped through standard input or given as --prompt_file parameter.
Content Safety The inference script also supports safety checks for both user prompt and model outputs. In particular, we use two packages, AuditNLG and Azure content safety.
Note If using Azure content Safety, please make sure to get the endpoint and API key as described here and add them as the following environment variables,CONTENT_SAFETY_ENDPOINT and CONTENT_SAFETY_KEY.
Copyright (c) Meta Platforms, Inc. and affiliates.
This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
import os
import sys
import time
import fire
import torch
from accelerate.utils import is_xpu_available
from llama_recipes.inference.model_utils import load_model, load_peft_model
from llama_recipes.inference.safety_utils import AgentType, get_safety_checker
from transformers import AutoTokenizer
def main(
model_name,
peft_model: str = None,
quantization: str = None, # Options: 4bit, 8bit
max_new_tokens=100, # The maximum numbers of tokens to generate
prompt_file: str = None,
seed: int = 42, # seed value for reproducibility
do_sample: bool = True, # Whether or not to use sampling ; use greedy decoding otherwise.
min_length: int = None, # The minimum length of the sequence to be generated, input prompt + min_new_tokens
use_cache: bool = True, # [optional] Whether or not the model should use the past last key/values attentions Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding.
top_p: float = 1.0, # [optional] If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to top_p or higher are kept for generation.
temperature: float = 1.0, # [optional] The value used to modulate the next token probabilities.
top_k: int = 50, # [optional] The number of highest probability vocabulary tokens to keep for top-k-filtering.
repetition_penalty: float = 1.0, # The parameter for repetition penalty. 1.0 means no penalty.
length_penalty: int = 1, # [optional] Exponential penalty to the length that is used with beam-based generation.
enable_azure_content_safety: bool = False, # Enable safety check with Azure content safety api
enable_sensitive_topics: bool = False, # Enable check for sensitive topics using AuditNLG APIs
enable_salesforce_content_safety: bool = True, # Enable safety check with Salesforce safety flan t5
enable_llamaguard_content_safety: bool = False,
max_padding_length: int = None, # the max padding length to be used with tokenizer padding the prompts.
use_fast_kernels: bool = False, # Enable using SDPA from PyTroch Accelerated Transformers, make use Flash Attention and Xformer memory-efficient kernels
share_gradio: bool = False, # Enable endpoint creation for gradio.live
**kwargs,
):
# Set the seeds for reproducibility
if is_xpu_available():
torch.xpu.manual_seed(seed)
else:
torch.cuda.manual_seed(seed)
torch.manual_seed(seed)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
model = load_model(model_name, quantization, use_fast_kernels, **kwargs)
if peft_model:
model = load_peft_model(model, peft_model)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
def inference(
user_prompt,
temperature,
top_p,
top_k,
max_new_tokens,
**kwargs,
):
safety_checker = get_safety_checker(
enable_azure_content_safety,
enable_sensitive_topics,
enable_salesforce_content_safety,
enable_llamaguard_content_safety,
)
# Safety check of the user prompt
safety_results = [check(user_prompt) for check in safety_checker]
are_safe = all([r[1] for r in safety_results])
if are_safe:
print("User prompt deemed safe.")
print(f"User prompt:\n{user_prompt}")
else:
print("User prompt deemed unsafe.")
for method, is_safe, report in safety_results:
if not is_safe:
print(method)
print(report)
print("Skipping the inference as the prompt is not safe.")
return # Exit the program with an error status
batch = tokenizer(
user_prompt,
truncation=True,
max_length=max_padding_length,
return_tensors="pt",
)
if is_xpu_available():
batch = {k: v.to("xpu") for k, v in batch.items()}
else:
batch = {k: v.to("cuda") for k, v in batch.items()}
start = time.perf_counter()
with torch.no_grad():
outputs = model.generate(
**batch,
max_new_tokens=max_new_tokens,
do_sample=do_sample,
top_p=top_p,
temperature=temperature,
min_length=min_length,
use_cache=use_cache,
top_k=top_k,
repetition_penalty=repetition_penalty,
length_penalty=length_penalty,
**kwargs,
)
e2e_inference_time = (time.perf_counter() - start) * 1000
print(f"the inference time is {e2e_inference_time} ms")
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Safety check of the model output
safety_results = [
check(output_text, agent_type=AgentType.AGENT, user_prompt=user_prompt)
for check in safety_checker
]
are_safe = all([r[1] for r in safety_results])
if are_safe:
print("User input and model output deemed safe.")
print(f"Model output:\n{output_text}")
return output_text
else:
print("Model output deemed unsafe.")
for method, is_safe, report in safety_results:
if not is_safe:
print(method)
print(report)
return None
if prompt_file is not None:
assert os.path.exists(
prompt_file
), f"Provided Prompt file does not exist {prompt_file}"
with open(prompt_file, "r") as f:
user_prompt = "\n".join(f.readlines())
inference(user_prompt, temperature, top_p, top_k, max_new_tokens)
elif not sys.stdin.isatty():
user_prompt = "\n".join(sys.stdin.readlines())
inference(user_prompt, temperature, top_p, top_k, max_new_tokens)
else:
try:
import gradio as gr
except ImportError:
raise ImportError("This part of the recipe requires gradio. Please run `pip install gradio`")
gr.Interface(
fn=inference,
inputs=[
gr.components.Textbox(
lines=9,
label="User Prompt",
placeholder="none",
),
gr.components.Slider(
minimum=0, maximum=1, value=1.0, label="Temperature"
),
gr.components.Slider(minimum=0, maximum=1, value=1.0, label="Top p"),
gr.components.Slider(
minimum=0, maximum=100, step=1, value=50, label="Top k"
),
gr.components.Slider(
minimum=1, maximum=2000, step=1, value=200, label="Max tokens"
),
],
outputs=[
gr.components.Textbox(
lines=5,
label="Output",
)
],
title="Meta Llama3 Playground",
description="https://github.com/meta-llama/llama-recipes",
).queue().launch(server_name="0.0.0.0", share=share_gradio)
if name == "main":
fire.Fire(main)
Since I am running locally, I downloaded the GitHub repository and run the code:
Set up the paths and configurations
MODEL_NAME = r'C:\Users\shermanhonsf\Desktop\Python\llama_develop\model\Llama3.1-1B-Instruct-HF-Model' # change to the path where your model is saved in Google Drive
PROMPT_FILE = r'C:\Users\shermanhonsf\Desktop\Python\llama_develop\llama-recipes-main\recipes\quickstart\inference\local_inference\samsum_prompt.txt' # adjust this as required
However the output:
use_fast_kernelsFalse
2024-11-12 10:01:59.200457: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
2024-11-12 10:01:59.986711: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\llama_recipes\model_checkpointing\checkpoint_handler.py:17: DeprecationWarning: torch.distributed._shard.checkpoint will be deprecated, use torch.distributed.checkpoint instead
from torch.distributed._shard.checkpoint import (
Traceback (most recent call last):
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connection.py", line 199, in _new_conn
sock = connection.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
raise err
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
sock.connect(sa)
TimeoutError: timed out
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connectionpool.py", line 490, in _make_request
raise new_e
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connectionpool.py", line 466, in _make_request
self._validate_conn(conn)
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connectionpool.py", line 1095, in _validate_conn
conn.connect()
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connection.py", line 693, in connect
self.sock = sock = self._new_conn()
^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connection.py", line 208, in _new_conn
raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x0000016A4877AD90>, 'Connection to huggingface.co timed out. (connect timeout=10)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\requests\adapters.py", line 667, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\connectionpool.py", line 843, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\urllib3\util\retry.py", line 519, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /Salesforce/safety-flan-t5-base/resolve/main/config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000016A4877AD90>, 'Connection to huggingface.co timed out. (connect timeout=10)'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 1376, in _get_metadata_or_catch_error
metadata = get_hf_file_metadata(
^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\utils_validators.py", line 114, in _inner_fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 1296, in get_hf_file_metadata
r = _request_wrapper(
^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 277, in _request_wrapper
response = _request_wrapper(
^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 300, in _request_wrapper
response = get_session().request(method=method, url=url, **params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\requests\sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\requests\sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\utils_http.py", line 93, in send
return super().send(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\requests\adapters.py", line 688, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /Salesforce/safety-flan-t5-base/resolve/main/config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000016A4877AD90>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 67be4ea3-99d8-4ef3-8751-489d306659af)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\transformers\utils\hub.py", line 403, in cached_file
resolved_file = hf_hub_download(
^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\utils_validators.py", line 114, in _inner_fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 862, in hf_hub_download
return _hf_hub_download_to_cache_dir(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 969, in _hf_hub_download_to_cache_dir
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\huggingface_hub\file_download.py", line 1487, in _raise_on_head_call_error
raise LocalEntryNotFoundError(
huggingface_hub.errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\shermanhonsf\Desktop\Python\llama_develop\llama-recipes-main\recipes\quickstart\inference\local_inference\inference.py", line 186, in
fire.Fire(main)
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\fire\core.py", line 135, in Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\fire\core.py", line 468, in _Fire
component, remaining_args = _CallAndUpdateTrace(
^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\fire\core.py", line 684, in _CallAndUpdateTrace
component = fn(*varargs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shermanhonsf\Desktop\Python\llama_develop\llama-recipes-main\recipes\quickstart\inference\local_inference\inference.py", line 143, in main
inference(user_prompt, temperature, top_p, top_k, max_new_tokens)
File "C:\Users\shermanhonsf\Desktop\Python\llama_develop\llama-recipes-main\recipes\quickstart\inference\local_inference\inference.py", line 75, in inference
safety_results = [check(user_prompt) for check in safety_checker]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shermanhonsf\Desktop\Python\llama_develop\llama-recipes-main\recipes\quickstart\inference\local_inference\inference.py", line 75, in
safety_results = [check(user_prompt) for check in safety_checker]
^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\llama_recipes\inference\safety_utils.py", line 49, in call
config = AutoConfig.from_pretrained("Salesforce/safety-flan-t5-base")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\transformers\models\auto\configuration_auto.py", line 1017, in from_pretrained
config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\transformers\configuration_utils.py", line 574, in get_config_dict
config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\transformers\configuration_utils.py", line 633, in _get_config_dict
resolved_config_file = cached_file(
^^^^^^^^^^^^
File "c:\Users\shermanhonsf\Desktop\Python.conda\Lib\site-packages\transformers\utils\hub.py", line 446, in cached_file
raise EnvironmentError(
OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like Salesforce/safety-flan-t5-base is not the path to a directory containing a file named config.json.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.
What happen? Is it suppose to run locally? Why I need internet connection? How to fix it?
The issue you're encountering stems from the fact that the inference script you're using is attempting to download a safety model (Salesforce/safety-flan-t5-base) from Hugging Face's website to perform content safety checks. Since you're running the script without an internet connection (or with internet restrictions), it fails to download the necessary model, resulting in the error you're seeing.
Here's what's happening:
Default Safety Checks: By default, the inference.py script enables several safety checks, one of which uses the Salesforce/safety-flan-t5-base model hosted on Hugging Face's website.
Attempt to Download the Safety Model: When you run the script, it tries to download this safety model because it's not available locally.
No Internet Connection: Since there's no internet connection (or it's restricted), the script fails to download the required model, resulting in a ConnectTimeoutError and ultimately an OSError.
Is it supposed to run locally?
Yes, the main Llama model is supposed to run locally. However, the safety checks rely on additional models that the script attempts to download if they are not already present on your local machine.
Why does it need an internet connection?
It needs an internet connection to download the safety models that are not already present locally. Specifically, the Salesforce/safety-flan-t5-base model is needed for one of the default safety checks.
How to fix it:
To fix this issue, you have a couple of options:
Disable the Safety Checks that Require Internet Access:
Modify your command to disable the safety checks that require downloading additional models. You can do this by setting the relevant parameters to False. Here's how you can adjust your command:
By setting these parameters to False, you prevent the script from attempting to download models from the internet for safety checks.
Download the Required Safety Models in Advance:
If you want to keep the safety checks enabled, you can download the necessary models beforehand using a computer with internet access. Here's how:
a. Download the Safety Model:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
model_name = "Salesforce/safety-flan-t5-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
model.save_pretrained("<path_to_save_model>")
tokenizer.save_pretrained("<path_to_save_model>")
Replace "<path_to_save_model>" with the directory where you want to save the model.
b. Point the Script to the Local Model:
Modify the inference.py script to load the safety model from the local path instead of attempting to download it. You might need to adjust the get_safety_checker function in safety_utils.py to point to your local model directory.
Ensure Internet Access:
If possible, run the script on a machine with internet access at least once so it can download the necessary models. After the models are cached locally, you should be able to run the script without an internet connection.
Summary:
The error occurs because the script tries to download a safety model from the internet.
To fix it, you can disable the safety checks that require internet access or download the necessary models in advance.
The main Llama model runs locally, but additional components (like safety checks) might need internet access if not set up properly.