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

fix: only mount model file to inference container #2280

Merged
merged 2 commits into from
Dec 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type { InferenceServer } from '@shared/src/models/IInference';
import { InferenceType } from '@shared/src/models/IInference';
import { llamacpp } from '../../assets/inference-images.json';
import type { ContainerProviderConnectionInfo } from '@shared/src/models/IContainerConnectionInfo';
import { join } from 'node:path';

vi.mock('@podman-desktop/api', () => ({
containerEngine: {
Expand Down Expand Up @@ -208,8 +209,8 @@ describe('perform', () => {
AutoRemove: false,
Mounts: [
{
Source: 'dummy-path',
Target: '/models',
Source: join('dummy-path', 'dummy-file.guff'),
Target: '/models/dummy-file.guff',
Type: 'bind',
},
],
Expand Down
16 changes: 11 additions & 5 deletions packages/backend/src/workers/provider/LlamaCppPython.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
} from '@podman-desktop/api';
import type { InferenceServerConfig } from '@shared/src/models/InferenceServerConfig';
import { InferenceProvider } from './InferenceProvider';
import { getModelPropertiesForEnvironment } from '../../utils/modelsUtils';
import { getLocalModelFile, getModelPropertiesForEnvironment } from '../../utils/modelsUtils';
import { DISABLE_SELINUX_LABEL_SECURITY_OPTION } from '../../utils/utils';
import { LABEL_INFERENCE_SERVER } from '../../utils/inferenceUtils';
import type { TaskRegistry } from '../../registries/TaskRegistry';
Expand Down Expand Up @@ -81,17 +81,23 @@ export class LlamaCppPython extends InferenceProvider {
[LABEL_INFERENCE_SERVER]: JSON.stringify(config.modelsInfo.map(model => model.id)),
};

const envs: string[] = [`MODEL_PATH=/models/${modelInfo.file.file}`, 'HOST=0.0.0.0', 'PORT=8000'];
envs.push(...getModelPropertiesForEnvironment(modelInfo));
// get model mount settings
const filename = getLocalModelFile(modelInfo);
const target = `/models/${modelInfo.file.file}`;

// mount the file directory to avoid adding other files to the containers
const mounts: MountConfig = [
{
Target: '/models',
Source: modelInfo.file.path,
Target: target,
Source: filename,
Type: 'bind',
},
];

// provide envs
const envs: string[] = [`MODEL_PATH=${target}`, 'HOST=0.0.0.0', 'PORT=8000'];
envs.push(...getModelPropertiesForEnvironment(modelInfo));

const deviceRequests: DeviceRequest[] = [];
const devices: Device[] = [];
let entrypoint: string | undefined = undefined;
Expand Down
30 changes: 30 additions & 0 deletions packages/backend/src/workers/provider/WhisperCpp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getImageInfo } from '../../utils/inferenceUtils';
import type { PodmanConnection } from '../../managers/podmanConnection';
import type { ContainerProviderConnectionInfo } from '@shared/src/models/IContainerConnectionInfo';
import { VMType } from '@shared/src/models/IPodman';
import { join } from 'node:path';

vi.mock('@podman-desktop/api', () => ({
containerEngine: {
Expand Down Expand Up @@ -235,4 +236,33 @@ test('provided connection should be used for pulling the image', async () => {
expect(getImageInfo).toHaveBeenCalledWith(connectionMock, 'localhost/whisper-cpp:custom', expect.any(Function));
expect(podmanConnection.getContainerProviderConnection).toHaveBeenCalledWith(connection);
expect(podmanConnection.findRunningContainerProviderConnection).not.toHaveBeenCalled();
// ensure the create container is called with appropriate arguments
expect(containerEngine.createContainer).toHaveBeenCalledWith('dummy-engine-id', {
Detach: true,
Env: ['MODEL_PATH=/models/random-file', 'HOST=0.0.0.0', 'PORT=8000'],
HostConfig: {
AutoRemove: false,
Mounts: [
{
Source: join('path-to-file', 'random-file'),
Target: '/models/random-file',
Type: 'bind',
},
],
PortBindings: {
'8000/tcp': [
{
HostPort: '8888',
},
],
},
SecurityOpt: ['label=disable'],
},
Image: 'dummy-image-id',
Labels: {
'ai-lab-inference-server': '["whisper-cpp"]',
api: 'http://localhost:8888/inference',
hello: 'world',
},
});
});
14 changes: 10 additions & 4 deletions packages/backend/src/workers/provider/WhisperCpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { ContainerProviderConnection, MountConfig } from '@podman-desktop/a
import { DISABLE_SELINUX_LABEL_SECURITY_OPTION } from '../../utils/utils';
import { whispercpp } from '../../assets/inference-images.json';
import type { PodmanConnection } from '../../managers/podmanConnection';
import { getLocalModelFile } from '../../utils/modelsUtils';

export class WhisperCpp extends InferenceProvider {
constructor(
Expand Down Expand Up @@ -67,17 +68,22 @@ export class WhisperCpp extends InferenceProvider {

if (!connection) throw new Error('no running connection could be found');

const imageInfo = await this.pullImage(connection, config.image ?? whispercpp.default, labels);
const envs: string[] = [`MODEL_PATH=/models/${modelInfo.file.file}`, 'HOST=0.0.0.0', 'PORT=8000'];
// get model mount settings
const filename = getLocalModelFile(modelInfo);
const target = `/models/${modelInfo.file.file}`;

// mount the file directory to avoid adding other files to the containers
const mounts: MountConfig = [
{
Target: '/models',
Source: modelInfo.file.path,
Target: target,
Source: filename,
Type: 'bind',
},
];

const imageInfo = await this.pullImage(connection, config.image ?? whispercpp.default, labels);
const envs: string[] = [`MODEL_PATH=${target}`, 'HOST=0.0.0.0', 'PORT=8000'];

labels['api'] = `http://localhost:${config.port}/inference`;

const containerInfo = await this.createContainer(
Expand Down