SKNahin commited on
Commit
185fc7b
1 Parent(s): 463b42b

Model save

Browse files
Files changed (3) hide show
  1. README.md +58 -0
  2. generation_config.json +12 -0
  3. modeling_functionary.py +110 -0
README.md ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - llama-factory
5
+ - generated_from_trainer
6
+ model-index:
7
+ - name: functionary-medium-v3.1-fine-llamafactory
8
+ results: []
9
+ ---
10
+
11
+ <!-- This model card has been generated automatically according to the information the Trainer had access to. You
12
+ should probably proofread and complete it, then remove this comment. -->
13
+
14
+ # functionary-medium-v3.1-fine-llamafactory
15
+
16
+ This model was trained from scratch on an unknown dataset.
17
+
18
+ ## Model description
19
+
20
+ More information needed
21
+
22
+ ## Intended uses & limitations
23
+
24
+ More information needed
25
+
26
+ ## Training and evaluation data
27
+
28
+ More information needed
29
+
30
+ ## Training procedure
31
+
32
+ ### Training hyperparameters
33
+
34
+ The following hyperparameters were used during training:
35
+ - learning_rate: 5e-05
36
+ - train_batch_size: 2
37
+ - eval_batch_size: 8
38
+ - seed: 42
39
+ - distributed_type: multi-GPU
40
+ - num_devices: 4
41
+ - gradient_accumulation_steps: 2
42
+ - total_train_batch_size: 16
43
+ - total_eval_batch_size: 32
44
+ - optimizer: Use OptimizerNames.ADAMW_TORCH with betas=(0.9,0.999) and epsilon=1e-08 and optimizer_args=No additional optimizer arguments
45
+ - lr_scheduler_type: cosine
46
+ - lr_scheduler_warmup_ratio: 0.001
47
+ - num_epochs: 2.0
48
+
49
+ ### Training results
50
+
51
+
52
+
53
+ ### Framework versions
54
+
55
+ - Transformers 4.46.1
56
+ - Pytorch 2.5.1+cu124
57
+ - Datasets 3.1.0
58
+ - Tokenizers 0.20.3
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": "4.46.1"
12
+ }
modeling_functionary.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright (c) 2024, MeetKai Inc. All rights reserved.
3
+ """PyTorch LLaMA model."""
4
+
5
+ import json
6
+ from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union
7
+
8
+ import torch
9
+ import torch.utils.checkpoint
10
+
11
+ from transformers.generation.configuration_utils import GenerationConfig
12
+ from transformers.generation.logits_process import LogitsProcessorList
13
+ from transformers.generation.stopping_criteria import StoppingCriteriaList
14
+ from transformers.generation.utils import (
15
+ GenerateBeamDecoderOnlyOutput,
16
+ GenerateBeamEncoderDecoderOutput,
17
+ GenerateDecoderOnlyOutput,
18
+ GenerateEncoderDecoderOutput
19
+ )
20
+ from transformers.models.llama.modeling_llama import LlamaForCausalLM
21
+ from transformers.utils import logging
22
+
23
+
24
+ if TYPE_CHECKING:
25
+ from transformers.modeling_utils import PreTrainedModel
26
+ from transformers.generation.streamers import BaseStreamer
27
+
28
+ logger = logging.get_logger(__name__)
29
+
30
+ GenerateNonBeamOutput = Union[GenerateDecoderOnlyOutput, GenerateEncoderDecoderOutput]
31
+ GenerateBeamOutput = Union[GenerateBeamDecoderOnlyOutput, GenerateBeamEncoderDecoderOutput]
32
+ GenerateOutput = Union[GenerateNonBeamOutput, GenerateBeamOutput]
33
+
34
+
35
+ class FunctionaryForCausalLM(LlamaForCausalLM):
36
+
37
+ def generate_tool_use(
38
+ self,
39
+ inputs: Optional[torch.Tensor] = None,
40
+ generation_config: Optional[GenerationConfig] = None,
41
+ logits_processor: Optional[LogitsProcessorList] = None,
42
+ stopping_criteria: Optional[StoppingCriteriaList] = None,
43
+ prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None,
44
+ synced_gpus: Optional[bool] = None,
45
+ assistant_model: Optional["PreTrainedModel"] = None,
46
+ streamer: Optional["BaseStreamer"] = None,
47
+ negative_prompt_ids: Optional[torch.Tensor] = None,
48
+ negative_prompt_attention_mask: Optional[torch.Tensor] = None,
49
+ **kwargs,
50
+ ) -> Union[GenerateOutput, torch.LongTensor]:
51
+
52
+ tokenizer = kwargs.pop("tokenizer", None) # Pull this out first, we use it to parse raw output
53
+
54
+ results = self.generate(
55
+ inputs=inputs,
56
+ generation_config=generation_config,
57
+ logits_processor=logits_processor,
58
+ stopping_criteria=stopping_criteria,
59
+ prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
60
+ synced_gpus=synced_gpus,
61
+ assistant_model=assistant_model,
62
+ streamer=streamer,
63
+ negative_prompt_ids=negative_prompt_ids,
64
+ negative_prompt_attention_mask=negative_prompt_attention_mask,
65
+ **kwargs,
66
+ )
67
+
68
+ input_ids = kwargs.pop("input_ids")
69
+ function_call_token = "<function="
70
+
71
+ correct_results = []
72
+ for input_id, result in zip(input_ids, results):
73
+ final_output_json = {"role": "assistant", "content": None, "tool_calls": None}
74
+ tool_calls = []
75
+ raw_output_str = tokenizer.decode(result[len(input_id):].cpu())
76
+ has_text = False if raw_output_str.startswith(function_call_token) else True
77
+ chunks = raw_output_str.split(function_call_token)
78
+ for i, chunk in enumerate(chunks):
79
+ if len(chunk) == 0:
80
+ continue
81
+
82
+ chunk = chunk.replace(tokenizer.pad_token, "")
83
+
84
+ if i == 0 and has_text is not False:
85
+ final_output_json["content"] = chunk.removesuffix("<|eom_id|>").removesuffix("<|eot_id|>")
86
+ else:
87
+ tool_calls.append(
88
+ {
89
+ "name": chunk[: chunk.index(">{")],
90
+ "arguments": chunk[chunk.index(">{") + 1: ].removesuffix("<|eom_id|>").removesuffix("</function>")
91
+ }
92
+ )
93
+ if len(tool_calls) > 0:
94
+ final_output_json["tool_calls"] = tool_calls
95
+ final_output_str = json.dumps(final_output_json, indent=4)
96
+ final_output_ids = tokenizer(final_output_str, add_special_tokens=False)["input_ids"]
97
+ correct_results.append(
98
+ torch.cat(
99
+ (result[:len(input_id)].cpu(), torch.tensor(final_output_ids))
100
+ )
101
+ )
102
+ max_len = max([tensor.shape[0] for tensor in correct_results])
103
+ correct_results = [
104
+ torch.nn.functional.pad(
105
+ correct_result, (0, max_len - correct_result.shape[0]), value=tokenizer.eos_token_id
106
+ ) for correct_result in correct_results
107
+ ]
108
+ correct_results = torch.stack(correct_results)
109
+
110
+ return correct_results