Skip to content
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

added accelerator detection #1628

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions backend/src/routes/api/accelerators/acceleratorUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { AcceleratorInfo, KubeFastifyInstance } from "../../../types"

const RESOURCE_TYPES = ["cpu", "memory", "pods", "ephemeral-storage", "hugepages-1Gi", "hugepages-2Mi", "attachable-volumes-aws-ebs"]

const getIdentifiersFromResources = (resources: {[key: string]: string} = {}) => {
return Object.entries(resources)
.filter(([key,]) => !RESOURCE_TYPES.includes(key))
.reduce<{[key: string]: number}>((identifiers, [key, value]) => {
identifiers[key] = isNaN(parseInt(value)) ? 0 : parseInt(value)
return identifiers
}, {})
}

export const getAcceleratorNumbers = async (fastify: KubeFastifyInstance): Promise<AcceleratorInfo> => (
fastify.kube.coreV1Api.listNode()
.then((res) => res.body.items.reduce<AcceleratorInfo>((info, node) => {
// reduce resources down to just the accelerators and their counts
const allocatable = getIdentifiersFromResources(node.status.allocatable)
const capacity = getIdentifiersFromResources(node.status.capacity)

// update the max count for each accelerator
Object.entries(allocatable).forEach(([key, value]) => (
info.available[key] = Math.max((info.available[key] || 0), value)
))

// update the total count for each accelerator
Object.entries(capacity).forEach(([key, value]) => (
info.total[key] = (info.total[key] || 0) + value
))


// update the allocated count for each accelerator
Object.entries(capacity).forEach(([key, value]) => (
info.allocated[key] = (info.allocated[key] || 0) + value - (allocatable[key] || 0)
))

// if any accelerators are available, the cluster is configured
const configured = info.configured || Object.values(info.available).some((value) => value > 0)

return {total: info.total, available: info.available, allocated: info.allocated, configured}
}, {configured: false, available: {}, total: {}, allocated: {}}))
.catch((e) => {
fastify.log.error(`Exception when listing cluster nodes: ${e}`);
return {configured: false, available: {}, total: {}, allocated: {}}
})
)
11 changes: 11 additions & 0 deletions backend/src/routes/api/accelerators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { KubeFastifyInstance, OauthFastifyRequest } from '../../../types';
import { getAcceleratorNumbers } from './acceleratorUtils';
import { logRequestDetails } from '../../../utils/fileUtils';

export default async (fastify: KubeFastifyInstance): Promise<void> => {
fastify.get('/', async (request: OauthFastifyRequest) => {
logRequestDetails(fastify, request);

return getAcceleratorNumbers(fastify);
});
};
3 changes: 3 additions & 0 deletions backend/src/routes/api/gpu/gpuUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const storage: { lastFetch: number; lastValue: GPUInfo } = {
lastFetch: 0,
};

/**
* @deprecated - use getAcceleratorNumbers instead
*/
export const getGPUNumber = async (fastify: KubeFastifyInstance): Promise<GPUInfo> => {
if (storage.lastFetch >= Date.now() - 30_000) {
fastify.log.info(`Returning cached gpu value (${JSON.stringify(storage)})`);
Expand Down
3 changes: 3 additions & 0 deletions backend/src/routes/api/gpu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { KubeFastifyInstance, OauthFastifyRequest } from '../../../types';
import { getGPUNumber } from './gpuUtils';
import { logRequestDetails } from '../../../utils/fileUtils';

/**
* @deprecated - use accelerators instead
*/
export default async (fastify: KubeFastifyInstance): Promise<void> => {
fastify.get('/', async (request: OauthFastifyRequest) => {
logRequestDetails(fastify, request);
Expand Down
8 changes: 8 additions & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,14 @@ export type GPUInfo = {
available: number;
autoscalers: gpuScale[];
};

export type AcceleratorInfo = {
configured: boolean;
available: {[key: string]: number};
total: {[key: string]: number};
allocated: {[key: string]: number};
}

export type EnvironmentVariable = EitherNotBoth<
{ value: string | number },
{ valueFrom: Record<string, unknown> }
Expand Down
Loading