Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
apsonawane committed Oct 21, 2024
1 parent 2687e60 commit 2854ff2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
29 changes: 15 additions & 14 deletions src/python/py/models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This folder contains the model builder for quickly creating optimized and quanti
- [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)
- [LoRA Models](#lora-models)
- [Unit Testing Models](#unit-testing-models)
- [Option 1: Use the model builder directly](#option-1-use-the-model-builder-directly)
- [Option 2: Edit the config.json file](#option-2-edit-the-configjson-file-on-disk-and-then-run-the-model-builder)
Expand Down Expand Up @@ -100,20 +101,6 @@ 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 int4 -e execution_provider -c cache_dir_to_store_temp_files
```

### LoRA Models

This scenario is where you have a finetuned model which supports LoRA and want to get optimized model. To use adapter model you have to pass the path of the model as input

path_to_local_folder_on_disk --> location where adapter_config.json and adapter weights are present

```
# From wheel:
python3 -m onnxruntime_genai.models.builder -o path_to_output_folder -p fp16 -e execution_provider -c cache_dir_to_store_temp_files --extra_options adapter_path=path_to_adapter_files
# From source:
python3 builder.py -o path_to_output_folder -p fp16 -e execution_provider -c cache_dir_to_store_temp_files --extra_options adapter_path=path_to_adapter_files
```

### GGUF Model

This scenario is where your float16/float32 GGUF model is already on disk.
Expand Down Expand Up @@ -231,6 +218,20 @@ 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_qdq=1
```

#### LoRA Models

This scenario is where you have a finetuned model with LoRA adapters and your model can be loaded in the Hugging Face style via [PEFT](https://github.com/huggingface/peft).

- path_to_local_folder_on_disk = location where base_model's weights are present

```
# From wheel:
python3 -m onnxruntime_genai.models.builder -i path_to_local_folder_on_disk -o path_to_output_folder -p fp16 -e execution_provider -c cache_dir_to_store_temp_files --extra_options adapter_path=path_to_adapter_files
# From source:
python3 builder.py -i path_to_local_folder_on_disk -o path_to_output_folder -p fp16 -e execution_provider -c cache_dir_to_store_temp_files --extra_options adapter_path=path_to_adapter_files
```

### Unit Testing Models

This scenario is where your PyTorch model is already downloaded locally (either in the default Hugging Face cache directory or in a local folder on disk). If it is not already downloaded locally, here is an example of how you can download it.
Expand Down
17 changes: 7 additions & 10 deletions src/python/py/models/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def make_matmul_int4_qdq(self, matmul, matmul_name, root_input, **kwargs):
self.make_value_info(matmul_output, self.io_dtype, shape=['batch_size', 'sequence_length', matmul.out_features])

return matmul_name

def make_matmul_lora(self, matmul, basename, root_input, **kwargs):
# Make nodes for the MatMul-LoRA subgraph
#
Expand Down Expand Up @@ -2006,7 +2007,7 @@ def make_model(self, input_path):

if "adapter_path" in self.extra_options:
from peft import PeftModel
model = PeftModel.from_pretrained(model, self.extra_options["adapter_path"])
model = PeftModel.from_pretrained(model, self.extra_options["adapter_path"], cache_dir=self.cache_dir, token=self.hf_token)

# Loop through model and map each module to ONNX/ORT ops
self.layer_id = 0
Expand Down Expand Up @@ -3050,18 +3051,13 @@ 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) and "config.json" in os.listdir(input_path) else model_name
hf_name = input_path if os.path.isdir(input_path) else model_name
hf_token = parse_hf_token(extra_options.get("hf_token", "true"))

is_peft = os.path.isdir(input_path) and "adapter_config.json" in os.listdir(input_path)
peft_model_name = input_path if is_peft else None
peft_config = None
if is_peft:
from peft import PeftConfig
peft_config = PeftConfig.from_pretrained(peft_model_name, token=hf_token, trust_remote_code=True, **extra_kwargs)

config = AutoConfig.from_pretrained(hf_name, token=hf_token, trust_remote_code=True, **extra_kwargs)
if is_peft:
if "adapter_path" in extra_options:
from peft import PeftConfig
peft_config = PeftConfig.from_pretrained(extra_options["adapter_path"], token=hf_token, trust_remote_code=True, **extra_kwargs)
config.update(peft_config.__dict__)

# Set input/output precision of ONNX model
Expand Down Expand Up @@ -3210,6 +3206,7 @@ def get_args():
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.
adapter_path = Path to folder on disk containing the adapter files (adapter_config.json and adapter model weights).
"""),
)

Expand Down

0 comments on commit 2854ff2

Please sign in to comment.