-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show the GPU requests and limits in workbench table
- Loading branch information
Showing
7 changed files
with
254 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
52 changes: 52 additions & 0 deletions
52
.../src/pages/notebookController/screens/server/__tests__/useAcceleratorCountWarning.spec.ts
This file contains 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,52 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import useAcceleratorCountWarning from '~/pages/notebookController/screens/server/useAcceleratorCountWarning'; | ||
|
||
type DetectedAccelerators = { | ||
available: Record<string, number>; | ||
}; | ||
|
||
jest.mock('../useDetectedAccelerators', () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})); | ||
|
||
describe('useAcceleratorCountWarning', () => { | ||
const mockUseDetectedAccelerators = require('../useDetectedAccelerators').default as jest.Mock< | ||
DetectedAccelerators[] | ||
>; | ||
|
||
beforeEach(() => { | ||
mockUseDetectedAccelerators.mockClear(); | ||
}); | ||
|
||
it('should return an empty string if identifier is not provided', () => { | ||
mockUseDetectedAccelerators.mockReturnValue([{ available: {} }]); | ||
|
||
const { result } = renderHook(() => useAcceleratorCountWarning()); | ||
expect(result.current).toBe(''); | ||
}); | ||
|
||
it('should return a message if no accelerator is detected for the given identifier', () => { | ||
mockUseDetectedAccelerators.mockReturnValue([{ available: {} }]); | ||
|
||
const { result } = renderHook(() => useAcceleratorCountWarning(undefined, 'test-id')); | ||
expect(result.current).toBe('No accelerator detected with the identifier "test-id".'); | ||
}); | ||
|
||
it('should return a message if newSize is greater than detected accelerator count', () => { | ||
mockUseDetectedAccelerators.mockReturnValue([{ available: { 'test-id': 2 } }]); | ||
|
||
const { result } = renderHook(() => useAcceleratorCountWarning(3, 'test-id')); | ||
expect(result.current).toBe('Only 2 accelerators detected.'); | ||
}); | ||
|
||
it('should return an empty string if newSize is less than or equal to detected accelerator count', () => { | ||
mockUseDetectedAccelerators.mockReturnValue([{ available: { 'test-id': 2 } }]); | ||
|
||
const { result: result1 } = renderHook(() => useAcceleratorCountWarning(2, 'test-id')); | ||
expect(result1.current).toBe(''); | ||
|
||
const { result: result2 } = renderHook(() => useAcceleratorCountWarning(1, 'test-id')); | ||
expect(result2.current).toBe(''); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
frontend/src/pages/notebookController/screens/server/useAcceleratorCountWarning.tsx
This file contains 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,27 @@ | ||
import useDetectedAccelerators from './useDetectedAccelerators'; | ||
|
||
const useAcceleratorCountWarning = (newSize?: number | string, identifier?: string): string => { | ||
const [detectedAccelerators] = useDetectedAccelerators(); | ||
|
||
if (!identifier) { | ||
return ''; | ||
} | ||
|
||
const detectedAcceleratorCount = Object.entries(detectedAccelerators.available).find( | ||
([id]) => identifier === id, | ||
)?.[1]; | ||
|
||
if (detectedAcceleratorCount === undefined) { | ||
return `No accelerator detected with the identifier "${identifier}".`; | ||
} | ||
|
||
if (newSize !== undefined && Number(newSize) > detectedAcceleratorCount) { | ||
return `Only ${detectedAcceleratorCount} accelerator${ | ||
detectedAcceleratorCount > 1 ? 's' : '' | ||
} detected.`; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
export default useAcceleratorCountWarning; |
82 changes: 81 additions & 1 deletion
82
frontend/src/pages/projects/screens/detail/notebooks/NotebookSizeDetails.tsx
This file contains 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 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
57 changes: 57 additions & 0 deletions
57
frontend/src/pages/projects/screens/detail/notebooks/__tests__/utils.spec.ts
This file contains 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,57 @@ | ||
import { ContainerResources } from '~/types'; | ||
import { extractAcceleratorResources } from '~/pages/projects/screens/detail/notebooks/utils'; | ||
|
||
describe('extractAcceleratorResources', () => { | ||
it('should return empty object if no resources are provided', () => { | ||
const result = extractAcceleratorResources(); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return empty object if resources do not contain accelerators', () => { | ||
const resources: ContainerResources = { | ||
limits: { cpu: '1000m', memory: '2Gi' }, | ||
requests: { cpu: '500m', memory: '1Gi' }, | ||
}; | ||
const result = extractAcceleratorResources(resources); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should extract accelerator resources from limits', () => { | ||
const resources: ContainerResources = { | ||
limits: { cpu: '1000m', memory: '2Gi', 'nvidia.com/gpu': '1' }, | ||
requests: { cpu: '500m', memory: '1Gi' }, | ||
}; | ||
const result = extractAcceleratorResources(resources); | ||
expect(result).toEqual({ | ||
limits: '1', | ||
requests: undefined, | ||
identifier: 'nvidia.com/gpu', | ||
}); | ||
}); | ||
|
||
it('should extract accelerator resources from requests', () => { | ||
const resources: ContainerResources = { | ||
limits: { cpu: '1000m', memory: '2Gi' }, | ||
requests: { cpu: '500m', memory: '1Gi', 'nvidia.com/gpu': '1' }, | ||
}; | ||
const result = extractAcceleratorResources(resources); | ||
expect(result).toEqual({ | ||
limits: undefined, | ||
requests: '1', | ||
identifier: 'nvidia.com/gpu', | ||
}); | ||
}); | ||
|
||
it('should extract accelerator resources from both limits and requests', () => { | ||
const resources: ContainerResources = { | ||
limits: { cpu: '1000m', memory: '2Gi', 'nvidia.com/gpu': '2' }, | ||
requests: { cpu: '500m', memory: '1Gi', 'nvidia.com/gpu': '1' }, | ||
}; | ||
const result = extractAcceleratorResources(resources); | ||
expect(result).toEqual({ | ||
limits: '2', | ||
requests: '1', | ||
identifier: 'nvidia.com/gpu', | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
frontend/src/pages/projects/screens/detail/notebooks/utils.ts
This file contains 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,23 @@ | ||
import { ContainerResources } from '~/types'; | ||
|
||
export type AcceleratorResources = { | ||
limits?: number | string; | ||
requests?: number | string; | ||
identifier?: string; | ||
}; | ||
|
||
export const extractAcceleratorResources = ( | ||
resources?: ContainerResources, | ||
): AcceleratorResources => { | ||
const findAcceleratorResource = (res?: { [key: string]: number | string | undefined }) => | ||
Object.entries(res || {}).find(([key]) => !['cpu', 'memory'].includes(key)); | ||
|
||
const limitsResource = findAcceleratorResource(resources?.limits); | ||
const requestsResource = findAcceleratorResource(resources?.requests); | ||
|
||
return { | ||
limits: limitsResource?.[1], | ||
requests: requestsResource?.[1], | ||
identifier: limitsResource?.[0] || requestsResource?.[0], | ||
}; | ||
}; |