Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra Option to To control HF Token #898

Merged
merged 16 commits into from
Sep 30, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/python/py/models/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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["hf_token"]) if "hf_token" in extra_options else True
nmoeller marked this conversation as resolved.
Show resolved Hide resolved
self.extra_options = extra_options

self.inputs = []
Expand Down Expand Up @@ -287,9 +288,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 @@ -349,7 +350,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 @@ -1759,7 +1760,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 @@ -2712,6 +2713,14 @@ def parse_extra_options(kv_items):
return kv_pairs


def parse_hf_token(hf_token : str):
if hf_token.lower() in ["true", "false"]:
# Convert string to boolean
return hf_token.lower() == "true"
# Return 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 @@ -2720,7 +2729,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["hf_token"]) if "hf_token" in extra_options else True
nmoeller marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2859,6 +2869,7 @@ 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 = 1 : Use this to enable or disable the HF Authentication, can be True,False or the token itself.
nmoeller marked this conversation as resolved.
Show resolved Hide resolved
"""),
)

Expand Down
Loading