File size: 12,150 Bytes
89493ee
 
 
 
 
 
36bc1d5
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26574d0
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c54318
 
 
 
 
 
 
 
 
89493ee
45d9469
 
1c54318
89493ee
 
 
 
 
36bc1d5
89493ee
36bc1d5
89493ee
 
45d9469
 
 
36bc1d5
45d9469
 
 
 
 
 
 
 
 
 
 
 
 
36bc1d5
45d9469
 
89493ee
45d9469
89493ee
 
36bc1d5
89493ee
 
 
 
 
 
 
f3f5382
89493ee
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
26574d0
 
 
 
89493ee
 
 
 
 
 
 
 
 
 
 
26574d0
 
 
 
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
26574d0
89493ee
 
 
 
 
 
f3f5382
89493ee
26574d0
 
 
 
 
 
89493ee
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { useState } from "react";
import Dropdown from "./Dropdown";
import NumberInput from "./NumberInput";
import InferencePopup from "./InferencePopup";
import { setModelLayers } from "../utils/modelCookies";
import { useAPI } from "../hooks/useAPI";
import { devLog, devError } from "../utils/devLogger";

const Options = ({
  models,
  selectedModel1,
  selectedModel2,
  setSelectedModel1,
  setSelectedModel2,
  numLayers,
  setNumLayers,
  layerRecipe,
  embeddingLambdas,
  linearLambdas,
  setModels,
  mergedName,
  setMergedName,
  isSpaceLoading,
}) => {
  const [isLoading, setIsLoading] = useState(false);
  const [mergeStatus, setMergeStatus] = useState("");
  const [isInferenceOpen, setIsInferenceOpen] = useState(false);
  const { mergeModels, checkTaskStatus } = useAPI();

  const handleMerge = async () => {
    if (
      !selectedModel1 ||
      !selectedModel2 ||
      layerRecipe.length === 0 ||
      !mergedName.trim()
    ) {
      setMergeStatus(
        "Error: Please select models, configure recipe, and provide a merged model name"
      );
      return;
    }

    setIsLoading(true);
    setMergeStatus("Merging models...");

    try {
      // Convert layer indices from 1-based (frontend) to 0-based (backend)
      const backendLayerRecipe = layerRecipe.map((layer) =>
        layer.map((block) => [
          block[0] - 1, // Convert sourceLayer from 1-based to 0-based
          block[1], // modelIndex stays the same
          block[2], // percentage stays the same
        ])
      );

      const mergeData = {
        model1_name: selectedModel1,
        model2_name: selectedModel2,
        layer_recipe: backendLayerRecipe,
        embedding_lambdas: embeddingLambdas,
        linear_lambdas: linearLambdas,
        merged_name: mergedName,
      };

      devLog("Starting merge with data:", mergeData);
      const taskId = await mergeModels(mergeData);
      devLog("Got merge task ID:", taskId);

      if (taskId) {
        checkTaskStatus(
          taskId,
          (taskResult) => {
            devLog("Merge result:", taskResult);
            if (taskResult.response) {
              setMergeStatus("Merge successful!");
              const newModelName = taskResult.response || mergedName;
              setModels((prev) => [...prev, newModelName]);
              setModelLayers(newModelName, numLayers);
            } else {
              setMergeStatus(
                `Merge failed: ${taskResult.error || "Unknown error"}`
              );
            }
            setIsLoading(false);
          },
          (error) => {
            devError("Merge task failed:", error);
            setMergeStatus(`Merge failed: ${error}`);
            setIsLoading(false);
          }
        );
      }
    } catch (error) {
      devError("Merge error:", error);
      setMergeStatus(`Error: ${error.message}`);
      setIsLoading(false);
    }
  };

  return (
    <div>
      <div className="p-6 border-2 border-primary-200 rounded-2xl bg-background shadow-xl fixed inset-y-4 left-4 w-[25rem] overflow-y-auto">
        <div className="flex items-center space-x-2 mb-6">
          <div className="w-8 h-8 bg-gradient-to-br from-primary-500 to-accent-500 rounded-lg flex items-center justify-center text-background">
            <svg
              xmlns="http://www.w3.org/2000/svg"
              width="24"
              height="24"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="2"
              strokeLinecap="round"
              strokeLinejoin="round"
            >
              <path d="M14 17H5" />
              <path d="M19 7h-9" />
              <circle cx="17" cy="17" r="3" />
              <circle cx="7" cy="7" r="3" />
            </svg>
          </div>
          <h2 className="text-xl font-bold text-foreground">
            Evolution Transformer Options
          </h2>
        </div>

        <div className="space-y-6">
          <Dropdown
            label="Model 1"
            selectedValue={selectedModel1}
            onSelect={setSelectedModel1}
            options={models}
            placeholder="Select base model..."
            loading={isSpaceLoading || models.length === 0}
            loadingMessage={
              isSpaceLoading ? "Space is starting up..." : "Loading models..."
            }
            emptyMessage="No models available"
            showSearch={true}
            searchPlaceholder="Search models..."
          />

          <Dropdown
            label="Model 2"
            selectedValue={selectedModel2}
            onSelect={setSelectedModel2}
            options={models}
            placeholder="Select target model..."
            loading={isSpaceLoading || models.length === 0}
            loadingMessage={
              isSpaceLoading ? "Space is starting up..." : "Loading models..."
            }
            emptyMessage="No models available"
            showSearch={true}
            searchPlaceholder="Search models..."
          />

          <NumberInput
            label="Number of Layers"
            value={numLayers}
            onChange={setNumLayers}
            min={1}
            max={48}
            className="w-full"
          />

          <div>
            <label className="block text-sm font-medium text-foreground mb-1">
              Merged Model Name
            </label>
            <input
              type="text"
              value={mergedName}
              onChange={(e) => setMergedName(e.target.value)}
              placeholder="Enter merged model name..."
              className="w-full px-3 py-2 border-2 border-secondary-200 rounded-lg focus:border-primary-500 focus:ring-2 focus:ring-primary-200 text-foreground placeholder-secondary-400 transition-all duration-200"
            />
          </div>

          <div className="p-4 rounded-xl border-2 border-secondary-200 bg-secondary-50">
            <div className="flex items-center space-x-2">
              <div
                className={`w-2 h-2 rounded-full ${
                  selectedModel1 && selectedModel2
                    ? "bg-green-500"
                    : "bg-red-500"
                }`}
              ></div>
              <span>
                Selected:{" "}
                {selectedModel1 && selectedModel2
                  ? "Ready to merge"
                  : "Incomplete"}
              </span>
            </div>
          </div>

          <div className="flex space-x-3">
            <div className="flex-1 p-4 rounded-xl border-2 border-accent-200 bg-accent-50">
              <div className="flex items-center space-x-2 mb-3">
                <div className="w-6 h-6 bg-gradient-to-br from-accent-500 to-secondary-500 rounded-lg flex items-center justify-center text-background">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    strokeWidth="2"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  >
                    <path d="m8 6 4-4 4 4" />
                    <path d="M12 2v10.3a4 4 0 0 1-1.172 2.872L4 22" />
                    <path d="m20 22-5-5" />
                  </svg>
                </div>
                <h3 className="text-sm font-semibold text-foreground">
                  Merge Models
                </h3>
              </div>

              <button
                onClick={handleMerge}
                disabled={
                  isSpaceLoading ||
                  isLoading ||
                  !selectedModel1 ||
                  !selectedModel2 ||
                  layerRecipe.length === 0 ||
                  !mergedName.trim()
                }
                className="w-full py-2.5 px-3 bg-gradient-to-r from-accent-500 to-primary-500 text-background font-medium rounded-lg hover:from-accent-600 hover:to-primary-600 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center space-x-2 text-sm"
              >
                {isSpaceLoading ? (
                  <>
                    <div className="animate-spin w-3.5 h-3.5 border-2 border-background border-t-transparent rounded-full"></div>
                    <span>Starting up...</span>
                  </>
                ) : isLoading ? (
                  <>
                    <div className="animate-spin w-3.5 h-3.5 border-2 border-background border-t-transparent rounded-full"></div>
                    <span>Merging...</span>
                  </>
                ) : (
                  <>
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      width="14"
                      height="14"
                      viewBox="0 0 24 24"
                      fill="none"
                      stroke="currentColor"
                      strokeWidth="2"
                      strokeLinecap="round"
                      strokeLinejoin="round"
                    >
                      <path d="m8 6 4-4 4 4" />
                      <path d="M12 2v10.3a4 4 0 0 1-1.172 2.872L4 22" />
                      <path d="m20 22-5-5" />
                    </svg>
                    <span>Merge</span>
                  </>
                )}
              </button>
            </div>

            <div className="flex-1 p-4 rounded-xl border-2 border-primary-200 bg-primary-50">
              <div className="flex items-center space-x-2 mb-3">
                <div className="w-6 h-6 bg-gradient-to-br from-secondary-500 to-primary-500 rounded-lg flex items-center justify-center text-background">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    strokeWidth="2"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  >
                    <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
                  </svg>
                </div>
                <h3 className="text-sm font-semibold text-foreground">
                  Test Inference
                </h3>
              </div>

              <button
                onClick={() => setIsInferenceOpen(true)}
                className="w-full py-2.5 px-3 bg-gradient-to-r from-secondary-500 to-primary-500 text-background font-medium rounded-lg hover:from-secondary-600 hover:to-primary-600 transition-all duration-200 flex items-center justify-center space-x-2 text-sm"
              >
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  width="14"
                  height="14"
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="currentColor"
                  strokeWidth="2"
                  strokeLinecap="round"
                  strokeLinejoin="round"
                >
                  <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
                </svg>
                <span>Inference</span>
              </button>
            </div>
          </div>

          {mergeStatus && (
            <div
              className={`p-2 rounded-lg text-sm font-medium ${
                mergeStatus.includes("successful")
                  ? "bg-green-100 text-green-800"
                  : mergeStatus.includes("Error") ||
                    mergeStatus.includes("failed")
                  ? "bg-red-100 text-red-800"
                  : "bg-blue-100 text-blue-800"
              }`}
            >
              {mergeStatus}
            </div>
          )}
        </div>
      </div>

      <InferencePopup
        isOpen={isInferenceOpen}
        onClose={() => setIsInferenceOpen(false)}
        models={models}
      />
    </div>
  );
};

export default Options;