Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
171 lines (136 loc) · 5.57 KB
/
Copy pathserver.js
File metadata and controls
171 lines (136 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const Anthropic = require('@anthropic-ai/sdk');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Create Anthropic client
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// Middleware
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/humanize', async (req, res) => {
try {
const { code, model } = req.body;
if (!code || !model) {
return res.status(400).json({ error: 'Both code and model are required' });
}
const response = await anthropic.messages.create({
model: "claude-3-7-sonnet-20250219",
max_tokens: 20000,
temperature: 1,
messages: [
{
role: "user",
content: [
{
type: "text",
text: `You are an expert AI assistant specializing in code transformation and optimization for machine learning models. Your task is to transform code generated by the pytorch compiler TT-NN backend into more human-friendly TT-NN code. This transformation should make the code easier to read and maintain while preserving its functionality.
You will be provided with two inputs:
<code>
${code}
</code>
<model>
${model}
</model>
The 'code' input contains the function to be transformed, and the 'model' input specifies the type of model (e.g., BERT) that the code represents.
Follow these steps to transform the code:
1. Rename all functions:
- Change 'ttnn_decorators_ttnn_*' to 'ttnn.*'
- For example:
a) 'ttnn_decorators_ttnn_experimental_view' becomes 'ttnn.experimental.view'
b) 'ttnn_decorators_ttnn_from_torch' becomes 'ttnn.from_torch'
2. Reorganize the code:
- Move all 'ttnn.from_torch' calls to the beginning of the function.
- Group related operations together (e.g., embedding, attention, feed-forward).
3. Analyze the code structure:
- Identify common components of the specified model type (e.g., for BERT: embeddings, self-attention, feed-forward layers).
- Determine the purpose of each section of the code.
4. Rename variables:
- Give meaningful names to variables based on their purpose and the model architecture.
- Use consistent naming conventions throughout the code.
5. Add comments:
- Provide brief explanations for each major section of the code.
- Describe the purpose of complex operations or model-specific components.
6. Format the output:
- Ensure proper indentation and line breaks for readability.
- Use consistent spacing around operators and function arguments.
In your analysis, please wrap your analysis inside <code_transformation> tags in your thinking block. Include the following steps:
1. List all functions that need to be renamed and their new names.
2. Outline the structure of the model before making any changes.
3. Identify and list all variables that need renaming.
4. For each major section of the code (e.g., embedding, attention, feed-forward), describe the transformations you'll make.
5. List the comments you plan to add and where you'll add them.
After completing the analysis, provide the transformed code in <transformed_code> tags.
Here's an example of how your response should be structured:
<code_transformation>
1. Functions to rename:
- 'ttnn_decorators_ttnn_from_torch' to 'ttnn.from_torch'
- 'ttnn_decorators_ttnn_reshape' to 'ttnn.reshape'
...
2. Model structure outline:
- Input processing
- Embedding layer
- Transformer layers (multiple)
- Self-attention mechanism
- Feed-forward network
- Output layer
3. Variables to rename:
- 'arg0_1' to 'input_ids'
- 'arg1_1' to 'token_type_ids'
...
4. Transformations for each section:
Embedding layer:
- Group 'ttnn.from_torch' calls
- Rename variables
- Reorganize operations
Self-attention mechanism:
- Identify and group attention operations
- Rename variables for query, key, value
...
5. Comments to add:
- "Process input and apply embeddings" before embedding section
- "Self-attention mechanism" before attention operations
- "Feed-forward network" before feed-forward operations
...
</code_transformation>
<transformed_code>
# Transformed code goes here
</transformed_code>
Remember to maintain the functionality of the original code while making it more readable and maintainable. Your final output should consist only of the transformed code and should not duplicate or rehash any of the work you did in the code_transformation section. Good luck!`
}
]
}
],
thinking: {
type: "enabled",
budget_tokens: 16000
}
});
// Extract the transformed code
const transformedCode = extractTransformedCode(response.content[0].text);
res.json({ transformedCode });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Failed to process request', details: error.message });
}
});
// Helper function to extract transformed code from Claude's response
function extractTransformedCode(responseText) {
const transformedCodeMatch = responseText.match(/<transformed_code>([\s\S]*?)<\/transformed_code>/);
if (transformedCodeMatch && transformedCodeMatch[1]) {
return transformedCodeMatch[1].trim();
}
return responseText; // Return full response if no transformed code found
}
// Start server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});