Skip to content

Commit

Permalink
fix: model recommendation label is not based on VRAM (#2517)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Mar 27, 2024
1 parent fe730fb commit 784af8c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from '@janhq/uikit'
import { InfoIcon } from 'lucide-react'

const NotEnoughRamLabel: React.FC = () => (
const NotEnoughMemoryLabel = ({ unit }: { unit: string }) => (
<Badge className="space-x-1 rounded-md" themes="danger">
<span>Not enough RAM</span>
<span>Not enough {unit}</span>
<Tooltip>
<TooltipTrigger>
<InfoIcon size={16} />
Expand All @@ -31,4 +31,4 @@ const NotEnoughRamLabel: React.FC = () => (
</Badge>
)

export default React.memo(NotEnoughRamLabel)
export default React.memo(NotEnoughMemoryLabel)
23 changes: 19 additions & 4 deletions web/containers/ModelLabel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { useAtomValue } from 'jotai'

import { useActiveModel } from '@/hooks/useActiveModel'

import NotEnoughRamLabel from './NotEnoughRamLabel'
import { useSettings } from '@/hooks/useSettings'

import NotEnoughMemoryLabel from './NotEnoughMemoryLabel'

import RecommendedLabel from './RecommendedLabel'

import SlowOnYourDeviceLabel from './SlowOnYourDeviceLabel'

import { totalRamAtom, usedRamAtom } from '@/helpers/atoms/SystemBar.atom'
import {
availableVramAtom,
totalRamAtom,
usedRamAtom,
} from '@/helpers/atoms/SystemBar.atom'

type Props = {
size: number
Expand All @@ -20,12 +26,21 @@ const ModelLabel: React.FC<Props> = ({ size }) => {
const { activeModel } = useActiveModel()
const totalRam = useAtomValue(totalRamAtom)
const usedRam = useAtomValue(usedRamAtom)
const availableVram = useAtomValue(availableVramAtom)
const { settings } = useSettings()

const getLabel = (size: number) => {
const minimumRamModel = size * 1.25
const availableRam = totalRam - usedRam + (activeModel?.metadata.size ?? 0)
const availableRam =
settings?.run_mode === 'gpu'
? availableVram * 1000000 // MB to bytes
: totalRam - usedRam + (activeModel?.metadata.size ?? 0)
if (minimumRamModel > totalRam) {
return <NotEnoughRamLabel />
return (
<NotEnoughMemoryLabel
unit={settings?.run_mode === 'gpu' ? 'VRAM' : 'RAM'}
/>
)
}
if (minimumRamModel < availableRam) {
return <RecommendedLabel />
Expand Down
1 change: 1 addition & 0 deletions web/helpers/atoms/SystemBar.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export const ramUtilitizedAtom = atom<number>(0)
export const gpusAtom = atom<Record<string, never>[]>([])

export const nvidiaTotalVramAtom = atom<number>(0)
export const availableVramAtom = atom<number>(0)
export const systemMonitorCollapseAtom = atom<boolean>(false)
10 changes: 10 additions & 0 deletions web/hooks/useGetSystemResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
nvidiaTotalVramAtom,
gpusAtom,
ramUtilitizedAtom,
availableVramAtom,
} from '@/helpers/atoms/SystemBar.atom'

export default function useGetSystemResources() {
Expand All @@ -24,6 +25,7 @@ export default function useGetSystemResources() {
const setUsedRam = useSetAtom(usedRamAtom)
const setCpuUsage = useSetAtom(cpuUsageAtom)
const setTotalNvidiaVram = useSetAtom(nvidiaTotalVramAtom)
const setAvailableVram = useSetAtom(availableVramAtom)
const setRamUtilitized = useSetAtom(ramUtilitizedAtom)

const getSystemResources = useCallback(async () => {
Expand Down Expand Up @@ -64,13 +66,21 @@ export default function useGetSystemResources() {
)
}
setTotalNvidiaVram(totalNvidiaVram)
setAvailableVram(
gpus.reduce(
(total: number, gpu: { memoryFree: string }) =>
total + Number(gpu.memoryFree),
0
)
)
}, [
setUsedRam,
setTotalRam,
setRamUtilitized,
setCpuUsage,
setGpus,
setTotalNvidiaVram,
setAvailableVram,
])

const watch = () => {
Expand Down
12 changes: 12 additions & 0 deletions web/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import { atom, useAtom } from 'jotai'

export const isShowNotificationAtom = atom<boolean>(false)

export type AppSettings = {
run_mode: 'cpu' | 'gpu' | undefined
notify: boolean
gpus_in_use: string[]
vulkan: boolean
gpus: string[]
}

export const useSettings = () => {
const [isGPUModeEnabled, setIsGPUModeEnabled] = useState(false) // New state for GPU mode
const [showNotification, setShowNotification] = useAtom(
isShowNotificationAtom
)
const [settings, setSettings] = useState<AppSettings>()

useEffect(() => {
readSettings().then((settings) => setSettings(settings as AppSettings))

setTimeout(() => validateSettings, 3000)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
Expand Down Expand Up @@ -79,5 +90,6 @@ export const useSettings = () => {
saveSettings,
setShowNotification,
validateSettings,
settings,
}
}
15 changes: 11 additions & 4 deletions web/screens/ExploreModels/ExploreModelItemHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { MainViewState } from '@/constants/screens'
import { useCreateNewThread } from '@/hooks/useCreateNewThread'
import useDownloadModel from '@/hooks/useDownloadModel'

import { useSettings } from '@/hooks/useSettings'

import { toGibibytes } from '@/utils/converter'

import { mainViewStateAtom } from '@/helpers/atoms/App.atom'
Expand All @@ -45,11 +47,11 @@ type Props = {
open: string
}

const getLabel = (size: number, ram: number) => {
const getLabel = (size: number, ram: number, unit: string = 'RAM') => {
if (size * 1.25 >= ram) {
return (
<Badge className="rounded-md" themes="danger">
Not enough RAM
Not enough {unit}
</Badge>
)
} else {
Expand All @@ -67,13 +69,14 @@ const ExploreModelItemHeader: React.FC<Props> = ({ model, onClick, open }) => {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const { requestCreateNewThread } = useCreateNewThread()
const totalRam = useAtomValue(totalRamAtom)
const { settings } = useSettings()

const nvidiaTotalVram = useAtomValue(nvidiaTotalVramAtom)
const setMainViewState = useSetAtom(mainViewStateAtom)

// Default nvidia returns vram in MB, need to convert to bytes to match the unit of totalRamW
let ram = nvidiaTotalVram * 1024 * 1024
if (ram === 0) {
if (ram === 0 || settings?.run_mode === 'cpu') {
ram = totalRam
}
const serverEnabled = useAtomValue(serverEnabledAtom)
Expand Down Expand Up @@ -158,7 +161,11 @@ const ExploreModelItemHeader: React.FC<Props> = ({ model, onClick, open }) => {
<span className="mr-4 font-semibold text-muted-foreground">
{toGibibytes(model.metadata.size)}
</span>
{getLabel(model.metadata.size, ram)}
{getLabel(
model.metadata.size,
ram,
settings?.run_mode === 'gpu' ? 'VRAM' : 'RAM'
)}

{downloadButton}
<ChevronDownIcon
Expand Down

0 comments on commit 784af8c

Please sign in to comment.