dsikka commited on
Commit
f1efac2
·
verified ·
1 Parent(s): 635542f

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
chat_template.jinja ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- bos_token }}
2
+ {%- if custom_tools is defined %}
3
+ {%- set tools = custom_tools %}
4
+ {%- endif %}
5
+ {%- if not tools_in_user_message is defined %}
6
+ {%- set tools_in_user_message = true %}
7
+ {%- endif %}
8
+ {%- if not date_string is defined %}
9
+ {%- set date_string = "26 Jul 2024" %}
10
+ {%- endif %}
11
+ {%- if not tools is defined %}
12
+ {%- set tools = none %}
13
+ {%- endif %}
14
+
15
+ {#- This block extracts the system message, so we can slot it into the right place. #}
16
+ {%- if messages[0]['role'] == 'system' %}
17
+ {%- set system_message = messages[0]['content']|trim %}
18
+ {%- set messages = messages[1:] %}
19
+ {%- else %}
20
+ {%- set system_message = "" %}
21
+ {%- endif %}
22
+
23
+ {#- System message + builtin tools #}
24
+ {{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
25
+ {%- if builtin_tools is defined or tools is not none %}
26
+ {{- "Environment: ipython\n" }}
27
+ {%- endif %}
28
+ {%- if builtin_tools is defined %}
29
+ {{- "Tools: " + builtin_tools | reject('equalto', 'code_interpreter') | join(", ") + "\n\n"}}
30
+ {%- endif %}
31
+ {{- "Cutting Knowledge Date: December 2023\n" }}
32
+ {{- "Today Date: " + date_string + "\n\n" }}
33
+ {%- if tools is not none and not tools_in_user_message %}
34
+ {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
35
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
36
+ {{- "Do not use variables.\n\n" }}
37
+ {%- for t in tools %}
38
+ {{- t | tojson(indent=4) }}
39
+ {{- "\n\n" }}
40
+ {%- endfor %}
41
+ {%- endif %}
42
+ {{- system_message }}
43
+ {{- "<|eot_id|>" }}
44
+
45
+ {#- Custom tools are passed in a user message with some extra guidance #}
46
+ {%- if tools_in_user_message and not tools is none %}
47
+ {#- Extract the first user message so we can plug it in here #}
48
+ {%- if messages | length != 0 %}
49
+ {%- set first_user_message = messages[0]['content']|trim %}
50
+ {%- set messages = messages[1:] %}
51
+ {%- else %}
52
+ {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
53
+ {%- endif %}
54
+ {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
55
+ {{- "Given the following functions, please respond with a JSON for a function call " }}
56
+ {{- "with its proper arguments that best answers the given prompt.\n\n" }}
57
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
58
+ {{- "Do not use variables.\n\n" }}
59
+ {%- for t in tools %}
60
+ {{- t | tojson(indent=4) }}
61
+ {{- "\n\n" }}
62
+ {%- endfor %}
63
+ {{- first_user_message + "<|eot_id|>"}}
64
+ {%- endif %}
65
+
66
+ {%- for message in messages %}
67
+ {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
68
+ {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }}
69
+ {%- elif 'tool_calls' in message %}
70
+ {%- if not message.tool_calls|length == 1 %}
71
+ {{- raise_exception("This model only supports single tool-calls at once!") }}
72
+ {%- endif %}
73
+ {%- set tool_call = message.tool_calls[0].function %}
74
+ {%- if builtin_tools is defined and tool_call.name in builtin_tools %}
75
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
76
+ {{- "<|python_tag|>" + tool_call.name + ".call(" }}
77
+ {%- for arg_name, arg_val in tool_call.arguments | items %}
78
+ {{- arg_name + '="' + arg_val + '"' }}
79
+ {%- if not loop.last %}
80
+ {{- ", " }}
81
+ {%- endif %}
82
+ {%- endfor %}
83
+ {{- ")" }}
84
+ {%- else %}
85
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
86
+ {{- '{"name": "' + tool_call.name + '", ' }}
87
+ {{- '"parameters": ' }}
88
+ {{- tool_call.arguments | tojson }}
89
+ {{- "}" }}
90
+ {%- endif %}
91
+ {%- if builtin_tools is defined %}
92
+ {#- This means we're in ipython mode #}
93
+ {{- "<|eom_id|>" }}
94
+ {%- else %}
95
+ {{- "<|eot_id|>" }}
96
+ {%- endif %}
97
+ {%- elif message.role == "tool" or message.role == "ipython" %}
98
+ {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
99
+ {%- if message.content is mapping or message.content is iterable %}
100
+ {{- message.content | tojson }}
101
+ {%- else %}
102
+ {{- message.content }}
103
+ {%- endif %}
104
+ {{- "<|eot_id|>" }}
105
+ {%- endif %}
106
+ {%- endfor %}
107
+ {%- if add_generation_prompt %}
108
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
109
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 128000,
8
+ "dtype": "bfloat16",
9
+ "eos_token_id": [
10
+ 128001,
11
+ 128008,
12
+ 128009
13
+ ],
14
+ "head_dim": 128,
15
+ "hidden_act": "silu",
16
+ "hidden_size": 4096,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 14336,
19
+ "max_position_embeddings": 131072,
20
+ "mlp_bias": false,
21
+ "model_type": "llama",
22
+ "num_attention_heads": 32,
23
+ "num_hidden_layers": 32,
24
+ "num_key_value_heads": 8,
25
+ "pad_token_id": null,
26
+ "pretraining_tp": 1,
27
+ "quantization_config": {
28
+ "config_groups": {
29
+ "group_0": {
30
+ "format": "nvfp4-pack-quantized",
31
+ "input_activations": {
32
+ "actorder": null,
33
+ "block_structure": null,
34
+ "dynamic": "local",
35
+ "group_size": 16,
36
+ "num_bits": 4,
37
+ "observer": "static_minmax",
38
+ "observer_kwargs": {},
39
+ "scale_dtype": "torch.float8_e4m3fn",
40
+ "strategy": "tensor_group",
41
+ "symmetric": true,
42
+ "type": "float",
43
+ "zp_dtype": null
44
+ },
45
+ "output_activations": null,
46
+ "targets": [
47
+ "Linear"
48
+ ],
49
+ "weights": {
50
+ "actorder": "static",
51
+ "block_structure": null,
52
+ "dynamic": false,
53
+ "group_size": 16,
54
+ "num_bits": 4,
55
+ "observer": "memoryless_minmax",
56
+ "observer_kwargs": {},
57
+ "scale_dtype": "torch.float8_e4m3fn",
58
+ "strategy": "tensor_group",
59
+ "symmetric": true,
60
+ "type": "float",
61
+ "zp_dtype": null
62
+ }
63
+ }
64
+ },
65
+ "format": "nvfp4-pack-quantized",
66
+ "global_compression_ratio": null,
67
+ "ignore": [
68
+ "lm_head"
69
+ ],
70
+ "kv_cache_scheme": null,
71
+ "quant_method": "compressed-tensors",
72
+ "quantization_status": "compressed",
73
+ "sparsity_config": {},
74
+ "transform_config": {},
75
+ "version": "0.17.2.a20260702"
76
+ },
77
+ "rms_norm_eps": 1e-05,
78
+ "rope_parameters": {
79
+ "factor": 8.0,
80
+ "high_freq_factor": 4.0,
81
+ "low_freq_factor": 1.0,
82
+ "original_max_position_embeddings": 8192,
83
+ "rope_theta": 500000.0,
84
+ "rope_type": "llama3"
85
+ },
86
+ "tie_word_embeddings": false,
87
+ "transformers_version": "5.12.0",
88
+ "use_cache": true,
89
+ "vocab_size": 128256
90
+ }
generation_config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 128000,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 128001,
6
+ 128008,
7
+ 128009
8
+ ],
9
+ "temperature": 0.6,
10
+ "top_p": 0.9,
11
+ "transformers_version": "5.12.0"
12
+ }
llama3_1_8b_nvfp4_gptq.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from compressed_tensors.offload import dispatch_model
2
+ from datasets import load_dataset
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ from llmcompressor import oneshot
6
+ from llmcompressor.modifiers.quantization.gptq import GPTQModifier
7
+
8
+ MODEL_ID = "RedHatAI/Llama-3.1-8B-Instruct"
9
+
10
+ # Load model
11
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
13
+
14
+ DATASET_ID = "HuggingFaceH4/ultrachat_200k"
15
+ DATASET_SPLIT = "train_sft"
16
+
17
+ # Select number of samples. 512 samples is recommended for GPTQ.
18
+ # Increasing the number of samples can improve accuracy.
19
+ # MoE models may benefit from more samples (512-1024) for better expert calibration.
20
+ NUM_CALIBRATION_SAMPLES = 512
21
+ MAX_SEQUENCE_LENGTH = 2048
22
+
23
+ # Load dataset and preprocess.
24
+ ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
25
+ ds = ds.shuffle(seed=42)
26
+
27
+
28
+ def preprocess(example):
29
+ return {
30
+ "text": tokenizer.apply_chat_template(
31
+ example["messages"],
32
+ tokenize=False,
33
+ )
34
+ }
35
+
36
+
37
+ ds = ds.map(preprocess)
38
+
39
+
40
+ # Tokenize inputs.
41
+ def tokenize(sample):
42
+ return tokenizer(
43
+ sample["text"],
44
+ padding=False,
45
+ max_length=MAX_SEQUENCE_LENGTH,
46
+ truncation=True,
47
+ add_special_tokens=False,
48
+ )
49
+
50
+
51
+ ds = ds.map(tokenize, remove_columns=ds.column_names)
52
+
53
+ # Configure the quantization algorithm and scheme.
54
+ # GPTQModifier provides better accuracy than QuantizationModifier
55
+ # at the cost of longer calibration time.
56
+ recipe = GPTQModifier(
57
+ targets="Linear",
58
+ scheme="NVFP4",
59
+ ignore=["lm_head"],
60
+ )
61
+
62
+ # Apply quantization.
63
+ oneshot(
64
+ model=model,
65
+ dataset=ds,
66
+ recipe=recipe,
67
+ max_seq_length=MAX_SEQUENCE_LENGTH,
68
+ num_calibration_samples=NUM_CALIBRATION_SAMPLES,
69
+ )
70
+
71
+ print("\n\n")
72
+ print("========== SAMPLE GENERATION ==============")
73
+ dispatch_model(model)
74
+ input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to(
75
+ model.device
76
+ )
77
+ output = model.generate(input_ids, max_new_tokens=100)
78
+ print(tokenizer.decode(output[0]))
79
+ print("==========================================\n\n")
80
+
81
+ # Save to disk in compressed-tensors format.
82
+ SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4-GPTQ-BlockSize16"
83
+ model.save_pretrained(SAVE_DIR)
84
+ tokenizer.save_pretrained(SAVE_DIR)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7b15bec81a97879f71ee71646b0b0f5daea17e4f1b1b9ffa1e5ba037b9fa084f
3
+ size 6027859608
recipe.yaml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ default_stage:
2
+ default_modifiers:
3
+ GPTQModifier:
4
+ targets: [Linear]
5
+ ignore: [lm_head]
6
+ scheme: NVFP4
7
+ bypass_divisibility_checks: false
8
+ block_size: 128
9
+ dampening_frac: 0.01
10
+ actorder: static
11
+ offload_hessians: false
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b9e4e7fb171f92fd137b777cc2714bf87d11576700a1dcd7a399e7bbe39537b
3
+ size 17209920
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<|begin_of_text|>",
4
+ "clean_up_tokenization_spaces": true,
5
+ "eos_token": "<|eot_id|>",
6
+ "is_local": false,
7
+ "local_files_only": false,
8
+ "model_input_names": [
9
+ "input_ids",
10
+ "attention_mask"
11
+ ],
12
+ "model_max_length": 131072,
13
+ "tokenizer_class": "TokenizersBackend"
14
+ }