-
Notifications
You must be signed in to change notification settings - Fork 19
Ryanontheinside/feat/LoRA #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4391fe8
feat: LoRA - backend
ryanontheinside adeadb0
feat: LoRA - frontend
ryanontheinside 28b2558
longlive lora -> lora strategy
ryanontheinside e135132
refactor: consistent naming for lora strats
ryanontheinside 4503321
fix: front end; decimal precision, overflow, lora location, tooltip
ryanontheinside 25489e0
documentation + remove vestigial yaml field
ryanontheinside 927814a
simplify permanent merge
ryanontheinside 1df6801
roll lora into manage cache
ryanontheinside 64097ee
scale update button -> thumb
ryanontheinside File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { useState, useEffect } from "react"; | ||
| import { Button } from "./ui/button"; | ||
| import { SliderWithInput } from "./ui/slider-with-input"; | ||
| import { Plus, X, RefreshCw } from "lucide-react"; | ||
| import { | ||
| Tooltip, | ||
| TooltipContent, | ||
| TooltipProvider, | ||
| TooltipTrigger, | ||
| } from "./ui/tooltip"; | ||
| import type { LoRAConfig, LoraMergeStrategy } from "../types"; | ||
| import { listLoRAFiles, type LoRAFileInfo } from "../lib/api"; | ||
| import { FilePicker } from "./ui/file-picker"; | ||
|
|
||
| interface LoRAManagerProps { | ||
| loras: LoRAConfig[]; | ||
| onLorasChange: (loras: LoRAConfig[]) => void; | ||
| disabled?: boolean; | ||
| isStreaming?: boolean; | ||
| loraMergeStrategy?: LoraMergeStrategy; | ||
| } | ||
|
|
||
| export function LoRAManager({ | ||
| loras, | ||
| onLorasChange, | ||
| disabled, | ||
| isStreaming = false, | ||
| loraMergeStrategy = "permanent_merge", | ||
| }: LoRAManagerProps) { | ||
| const [availableLoRAs, setAvailableLoRAs] = useState<LoRAFileInfo[]>([]); | ||
| const [isLoadingLoRAs, setIsLoadingLoRAs] = useState(false); | ||
| const [localScales, setLocalScales] = useState<Record<string, number>>({}); | ||
|
|
||
| const loadAvailableLoRAs = async () => { | ||
| setIsLoadingLoRAs(true); | ||
| try { | ||
| const response = await listLoRAFiles(); | ||
| setAvailableLoRAs(response.lora_files); | ||
| } catch (error) { | ||
| console.error("loadAvailableLoRAs: Failed to load LoRA files:", error); | ||
| } finally { | ||
| setIsLoadingLoRAs(false); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| loadAvailableLoRAs(); | ||
| }, []); | ||
|
|
||
| // Sync localScales from loras prop when it changes from outside | ||
| useEffect(() => { | ||
| const newLocalScales: Record<string, number> = {}; | ||
| loras.forEach(lora => { | ||
| newLocalScales[lora.id] = lora.scale; | ||
| }); | ||
| setLocalScales(newLocalScales); | ||
| }, [loras]); | ||
|
|
||
| const handleAddLora = () => { | ||
| const newLora: LoRAConfig = { | ||
| id: crypto.randomUUID(), | ||
| path: "", | ||
| scale: 1.0, | ||
| }; | ||
| onLorasChange([...loras, newLora]); | ||
| }; | ||
|
|
||
| const handleRemoveLora = (id: string) => { | ||
| onLorasChange(loras.filter(lora => lora.id !== id)); | ||
| }; | ||
|
|
||
| const handleLoraChange = (id: string, updates: Partial<LoRAConfig>) => { | ||
| onLorasChange( | ||
| loras.map(lora => (lora.id === id ? { ...lora, ...updates } : lora)) | ||
| ); | ||
| }; | ||
|
|
||
| const handleLocalScaleChange = (id: string, scale: number) => { | ||
| setLocalScales(prev => ({ ...prev, [id]: scale })); | ||
| }; | ||
|
|
||
| const handleScaleCommit = (id: string, scale: number) => { | ||
| handleLoraChange(id, { scale }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="space-y-2"> | ||
| <div className="flex items-center justify-between"> | ||
| <h3 className="text-sm font-medium">LoRA Adapters</h3> | ||
| <div className="flex gap-1"> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={loadAvailableLoRAs} | ||
| disabled={disabled || isLoadingLoRAs} | ||
| className="h-6 px-2" | ||
| title="Refresh LoRA list" | ||
| > | ||
| <RefreshCw | ||
| className={`h-3 w-3 ${isLoadingLoRAs ? "animate-spin" : ""}`} | ||
| /> | ||
| </Button> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={handleAddLora} | ||
| disabled={disabled || isStreaming} | ||
| className="h-6 px-2" | ||
| title={ | ||
| isStreaming ? "Cannot add LoRAs while streaming" : "Add LoRA" | ||
| } | ||
| > | ||
| <Plus className="h-3 w-3" /> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {loras.length === 0 && ( | ||
| <p className="text-xs text-muted-foreground"> | ||
| No LoRA adapters configured. Add LoRA files to models/lora directory. | ||
| </p> | ||
| )} | ||
|
|
||
| <div className="space-y-2"> | ||
| {loras.map(lora => ( | ||
| <div | ||
| key={lora.id} | ||
| className="rounded-lg border bg-card p-3 space-y-2" | ||
| > | ||
| <div className="flex items-center justify-between gap-2"> | ||
| <div className="flex-1 min-w-0"> | ||
| <FilePicker | ||
| value={lora.path} | ||
| onChange={path => handleLoraChange(lora.id, { path })} | ||
| files={availableLoRAs} | ||
| disabled={disabled || isStreaming} | ||
| placeholder="Select LoRA file" | ||
| emptyMessage="No LoRA files found" | ||
| /> | ||
| </div> | ||
| <Button | ||
| size="sm" | ||
| variant="ghost" | ||
| onClick={() => handleRemoveLora(lora.id)} | ||
| disabled={disabled || isStreaming} | ||
| className="h-6 w-6 p-0 shrink-0" | ||
| title={ | ||
| isStreaming | ||
| ? "Cannot remove LoRAs while streaming" | ||
| : "Remove LoRA" | ||
| } | ||
| > | ||
| <X className="h-3 w-3" /> | ||
| </Button> | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-2"> | ||
| <span className="text-xs text-muted-foreground w-12">Scale:</span> | ||
| <TooltipProvider> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <div className="flex-1 min-w-0"> | ||
| <SliderWithInput | ||
yondonfu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| value={localScales[lora.id] ?? lora.scale} | ||
| onValueChange={value => { | ||
| handleLocalScaleChange(lora.id, value); | ||
| }} | ||
| onValueCommit={value => { | ||
| handleScaleCommit(lora.id, value); | ||
| }} | ||
| min={-10} | ||
| max={10} | ||
| step={0.1} | ||
| incrementAmount={0.1} | ||
| disabled={ | ||
| disabled || | ||
| (isStreaming && | ||
| loraMergeStrategy === "permanent_merge") | ||
| } | ||
| className="flex-1" | ||
| valueFormatter={v => Math.round(v * 10) / 10} | ||
| /> | ||
| </div> | ||
| </TooltipTrigger> | ||
| <TooltipContent> | ||
| <p className="text-xs"> | ||
| {isStreaming && loraMergeStrategy === "permanent_merge" | ||
| ? "Runtime adjustment is disabled with Permanent Merge strategy. LoRA scales are fixed at load time." | ||
| : "Adjust LoRA strength. Updates automatically when you release the slider or use +/- buttons. 0.0 = no effect, 1.0 = full strength"} | ||
| </p> | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| </TooltipProvider> | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.