Caslow commited on
Commit
2868b03
·
1 Parent(s): ca0c1f4
Files changed (1) hide show
  1. inference.py +137 -0
inference.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from unsloth import FastLanguageModel
2
+ # from transformers import TextStreamer
3
+ # from typing import Tuple, List, Dict
4
+ # import torch
5
+
6
+ # def load_model(
7
+ # model_name: str,
8
+ # max_seq_length: int,
9
+ # dtype: torch.dtype,
10
+ # load_in_4bit: bool
11
+ # ) -> Tuple[FastLanguageModel, any]:
12
+ # """
13
+ # Load and initialize the language model for inference.
14
+
15
+ # Args:
16
+ # model_name (str): Name of the pre-trained model to load
17
+ # max_seq_length (int): Maximum sequence length for the model
18
+ # dtype (torch.dtype): Data type for model weights
19
+ # load_in_4bit (bool): Whether to load model in 4-bit quantization
20
+
21
+ # Returns:
22
+ # Tuple[FastLanguageModel, any]: Tuple containing the model and tokenizer
23
+ # """
24
+ # model, tokenizer = FastLanguageModel.from_pretrained(
25
+ # model_name=model_name,
26
+ # max_seq_length=max_seq_length,
27
+ # dtype=dtype,
28
+ # load_in_4bit=load_in_4bit,
29
+ # )
30
+ # FastLanguageModel.for_inference(model)
31
+ # return model, tokenizer
32
+
33
+ # def prepare_input(
34
+ # messages: List[Dict[str, str]],
35
+ # tokenizer: any,
36
+ # device: str = "cuda"
37
+ # ) -> torch.Tensor:
38
+ # """
39
+ # Prepare input for the model by applying chat template and tokenization.
40
+
41
+ # Args:
42
+ # messages (List[Dict[str, str]]): List of message dictionaries
43
+ # tokenizer: The tokenizer instance
44
+ # device (str): Device to load tensors to ("cuda" or "cpu")
45
+
46
+ # Returns:
47
+ # torch.Tensor: Prepared input tensor
48
+ # """
49
+ # return tokenizer.apply_chat_template(
50
+ # messages,
51
+ # tokenize=True,
52
+ # add_generation_prompt=True,
53
+ # return_tensors="pt"
54
+ # ).to(device)
55
+
56
+ # def generate_response(
57
+ # model: FastLanguageModel,
58
+ # inputs: torch.Tensor,
59
+ # tokenizer: any,
60
+ # max_new_tokens: int = 2000,
61
+ # temperature: float = 1.5,
62
+ # min_p: float = 0.1,
63
+ # skip_prompt: bool = True
64
+ # ) -> str:
65
+ # """
66
+ # Generate response using the model.
67
+
68
+ # Args:
69
+ # model (FastLanguageModel): The language model
70
+ # inputs (torch.Tensor): Prepared input tensor
71
+ # tokenizer: The tokenizer instance
72
+ # max_new_tokens (int): Maximum number of tokens to generate
73
+ # temperature (float): Sampling temperature
74
+ # min_p (float): Minimum probability for nucleus sampling
75
+ # skip_prompt (bool): Whether to skip prompt in output
76
+
77
+ # Returns:
78
+ # str: Generated response
79
+ # """
80
+ # text_streamer = TextStreamer(tokenizer, skip_prompt=skip_prompt)
81
+ # outputs = model.generate(
82
+ # input_ids=inputs,
83
+ # streamer=text_streamer,
84
+ # max_new_tokens=max_new_tokens,
85
+ # use_cache=True,
86
+ # temperature=temperature,
87
+ # min_p=min_p
88
+ # )
89
+ # return outputs
90
+
91
+ # def main(
92
+ # USER_INPUT_CODE = "program sum_of_numbers\n implicit none\n integer :: n, i, sum\n\n ! Initialize variables\n sum = 0\n\n ! Get user input\n print *, \"Enter a positive integer:\"\n read *, n\n\n ! Calculate the sum of numbers from 1 to n\n do i = 1, n\n sum = sum + i\n end do\n\n ! Print the result\n print *, \"The sum of numbers from 1 to\", n, \"is\", sum\nend program sum_of_numbers",
93
+ # USER_INPUT_EXPLANATION = "The provided Fortran code snippet is a program that calculates the sum of integers from 1 to n, where n is provided by the user. It uses a simple procedural approach, including variable declarations, input handling, and a loop for the summation.\n\nThe functionality of the program is explained in detail in the elaboration. The program starts by initializing variables and prompting the user for input. It then calculates the sum using a do loop, iterating from 1 to n, and accumulating the result in a variable. Finally, it prints the computed sum to the console.\n\nThis program demonstrates a straightforward application of Fortran's capabilities for handling loops and basic arithmetic operations. It is a clear example of how Fortran can be used to solve mathematical problems involving user interaction and iterative computations.",
94
+ # MODEL_PATH = "lora_model"
95
+ # ):
96
+ # """
97
+ # Main function to demonstrate the inference pipeline.
98
+ # """
99
+ # # Import configuration
100
+ # from config import max_seq_length, dtype, load_in_4bit
101
+
102
+ # # Example messages
103
+ # messages = [
104
+ # {
105
+ # "role": "user",
106
+ # "content": str("[Fortran Code]") + str(USER_INPUT_CODE) + str("[Fortran Code Explain]") + str(USER_INPUT_EXPLANATION)
107
+ # }
108
+ # ]
109
+
110
+ # # Load model
111
+ # model, tokenizer = load_model(
112
+ # model_name=MODEL_PATH,
113
+ # max_seq_length=max_seq_length,
114
+ # dtype=dtype,
115
+ # load_in_4bit=load_in_4bit
116
+ # )
117
+
118
+ # # Prepare input
119
+ # inputs = prepare_input(messages, tokenizer)
120
+
121
+ # # Generate response
122
+ # return generate_response(model, inputs, tokenizer)
123
+
124
+ # if __name__ == "__main__":
125
+ # # YOUR_FORTRAN_CODE_HERE
126
+ # USER_INPUT_CODE = "program sum_of_numbers\n implicit none\n integer :: n, i, sum\n\n ! Initialize variables\n sum = 0\n\n ! Get user input\n print *, \"Enter a positive integer:\"\n read *, n\n\n ! Calculate the sum of numbers from 1 to n\n do i = 1, n\n sum = sum + i\n end do\n\n ! Print the result\n print *, \"The sum of numbers from 1 to\", n, \"is\", sum\nend program sum_of_numbers"
127
+
128
+ # # YOUR_EXPLANATION_HERE
129
+ # USER_INPUT_EXPLANATION = "The provided Fortran code snippet is a program that calculates the sum of integers from 1 to n, where n is provided by the user. It uses a simple procedural approach, including variable declarations, input handling, and a loop for the summation.\n\nThe functionality of the program is explained in detail in the elaboration. The program starts by initializing variables and prompting the user for input. It then calculates the sum using a do loop, iterating from 1 to n, and accumulating the result in a variable. Finally, it prints the computed sum to the console.\n\nThis program demonstrates a straightforward application of Fortran's capabilities for handling loops and basic arithmetic operations. It is a clear example of how Fortran can be used to solve mathematical problems involving user interaction and iterative computations."
130
+
131
+ # # YOUR_MODEL_PATH_HERE
132
+ # MODEL_PATH = "lora_model"
133
+
134
+ # main(USER_INPUT_CODE, USER_INPUT_EXPLANATION, MODEL_PATH)
135
+
136
+ def testing(input):
137
+ return input + " is the input"