Skip to content

Commit

Permalink
Added extra Option to To control HF Token (#898)
Browse files Browse the repository at this point in the history
fixes #830.

I've added a new option `hf_token` in the extra options, the user can
pass a Token or True or False.
Also i upgraded `use_auth_token` to `token`.

---------

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
  • Loading branch information
nmoeller and kunal-vaishnavi authored Sep 30, 2024
1 parent 8d28f20 commit 4332f98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/python/py/models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This folder contains the model builder for quickly creating optimized and quanti
- [Exclude Language Modeling Head](#exclude-language-modeling-head)
- [Enable Cuda Graph](#enable-cuda-graph)
- [Use 8 Bits Quantization in QMoE](#use-8-bits-quantization-in-qmoe)
- [Hugging Face Authentication](#hugging-face-authentication)
- [Use QDQ Pattern for Quantization](#use-qdq-pattern-for-quantization)
- [Unit Testing Models](#unit-testing-models)
- [Option 1: Use the model builder directly](#option-1-use-the-model-builder-directly)
Expand Down Expand Up @@ -186,6 +187,23 @@ python3 -m onnxruntime_genai.models.builder -i path_to_local_folder_on_disk -o p
python3 builder.py -i path_to_local_folder_on_disk -o path_to_output_folder -p precision -e execution_provider -c cache_dir_to_store_temp_files --extra_options use_8bits_moe=1
```

#### Hugging Face Authentication

This scenario is for when you need to disable the Hugging Face authentication or use a different authentication token than the one stored in [huggingface-cli login](https://huggingface.co/docs/huggingface_hub/main/en/guides/cli#huggingface-cli-login).

Possible values :

- hf_token=False
- hf_token=<user_token>

```
# From wheel:
python3 -m onnxruntime_genai.models.builder -m model_name -o path_to_output_folder -p precision -e execution_provider -c cache_dir_for_hf_files --extra_options hf_token=False
# From source:
python3 builder.py -m model_name -o path_to_output_folder -p precision -e execution_provider -c cache_dir_for_hf_files --extra_options hf_token=False
```

#### Use QDQ Pattern for Quantization

This scenario is for when you want to use the QDQ pattern (DequantizeLinear + MatMul) instead of the MatMulNBits operator when quantizing the model to 4 bits.
Expand Down
31 changes: 26 additions & 5 deletions src/python/py/models/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, config, io_dtype, onnx_dtype, ep, cache_dir, extra_options):

self.cache_dir = cache_dir
self.filename = extra_options["filename"] if "filename" in extra_options else "model.onnx"
self.hf_token = parse_hf_token(extra_options.get("hf_token", "true"))
self.extra_options = extra_options

self.inputs = []
Expand Down Expand Up @@ -288,9 +289,9 @@ def __init__(self, config, io_dtype, onnx_dtype, ep, cache_dir, extra_options):

def make_genai_config(self, model_name_or_path, extra_kwargs, out_dir):
try:
config = GenerationConfig.from_pretrained(model_name_or_path, use_auth_token=True, trust_remote_code=True, **extra_kwargs)
config = GenerationConfig.from_pretrained(model_name_or_path, token=self.hf_token, trust_remote_code=True, **extra_kwargs)
except:
config = AutoConfig.from_pretrained(model_name_or_path, use_auth_token=True, trust_remote_code=True, **extra_kwargs)
config = AutoConfig.from_pretrained(model_name_or_path, token=self.hf_token, trust_remote_code=True, **extra_kwargs)
inputs = dict(zip(self.input_names, self.input_names))
inputs.update({
"past_key_names": "past_key_values.%d.key",
Expand Down Expand Up @@ -350,7 +351,7 @@ def make_genai_config(self, model_name_or_path, extra_kwargs, out_dir):
json.dump(genai_config, f, indent=4)

def save_processing(self, model_name_or_path, extra_kwargs, out_dir):
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_auth_token=True, trust_remote_code=True, **extra_kwargs)
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, token=self.hf_token, trust_remote_code=True, **extra_kwargs)
print(f"Saving processing files in {out_dir} for GenAI")
tokenizer.save_pretrained(out_dir)

Expand Down Expand Up @@ -1819,7 +1820,7 @@ def make_model(self, input_path):
else:
# Load PyTorch model
extra_kwargs = {"num_hidden_layers": self.num_layers} if "num_hidden_layers" in self.extra_options else {}
model = AutoModelForCausalLM.from_pretrained(self.model_name_or_path, cache_dir=self.cache_dir, use_auth_token=True, trust_remote_code=True, **extra_kwargs)
model = AutoModelForCausalLM.from_pretrained(self.model_name_or_path, cache_dir=self.cache_dir, token=self.hf_token, trust_remote_code=True, **extra_kwargs)

# Loop through model and map each module to ONNX/ORT ops
self.layer_id = 0
Expand Down Expand Up @@ -2772,6 +2773,23 @@ def parse_extra_options(kv_items):
return kv_pairs


def parse_hf_token(hf_token):
"""
Returns the authentication token needed for Hugging Face.
Token is obtained either from the user or the environment.
"""
if hf_token.lower() in {"false", "0"}:
# Default is `None` for disabling authentication
return None

if hf_token.lower() in {"true", "1"}:
# Return token in environment
return True

# Return user-provided token as string
return hf_token


def create_model(model_name, input_path, output_dir, precision, execution_provider, cache_dir, **extra_options):
# Create cache and output directories
os.makedirs(output_dir, exist_ok=True)
Expand All @@ -2780,7 +2798,8 @@ def create_model(model_name, input_path, output_dir, precision, execution_provid
# Load model config
extra_kwargs = {} if os.path.isdir(input_path) else {"cache_dir": cache_dir}
hf_name = input_path if os.path.isdir(input_path) else model_name
config = AutoConfig.from_pretrained(hf_name, use_auth_token=True, trust_remote_code=True, **extra_kwargs)
hf_token = parse_hf_token(extra_options.get("hf_token", "true"))
config = AutoConfig.from_pretrained(hf_name, token=hf_token, trust_remote_code=True, **extra_kwargs)

# Set input/output precision of ONNX model
io_dtype = TensorProto.FLOAT if precision in {"int8", "fp32"} or (precision == "int4" and execution_provider == "cpu") else TensorProto.FLOAT16
Expand Down Expand Up @@ -2919,6 +2938,8 @@ def get_args():
is the prerequisite for the CUDA graph to be used correctly. It is not guaranteed that cuda graph be enabled as it depends on the model
and the graph structure.
use_8bits_moe = 1 : Use 8-bit quantization for MoE layers. Default is using 4-bit quantization.
hf_token = false/token: Use this to disable authentication with Hugging Face or provide a custom authentication token that differs from the one stored in your environment. Default behavior is to use the authentication token stored by `huggingface-cli login`.
If you have already authenticated via `huggingface-cli login`, you do not need to use this flag because Hugging Face has already stored your authentication token for you.
use_qdq = 1 : Use the QDQ decomposition for quantized MatMul instead of the MatMulNBits operator.
"""),
)
Expand Down

0 comments on commit 4332f98

Please sign in to comment.