Skip to content
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
411 changes: 165 additions & 246 deletions packages/sdk/bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/sdk/examples/diffusion-flux2-klein.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const { progressStream, outputs, stats } = diffusion({
height: 512,
steps: 20,
guidance: 3.5,
cfg_scale: 1,
seed: -1,
});

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/examples/diffusion-txt2img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const { progressStream, outputs, stats } = diffusion({
width: 512,
height: 512,
steps: 20,
cfg_scale: 7.0,
guidance: 3.5,
cfg_scale: 1,
seed: -1,
});

Expand Down
7 changes: 3 additions & 4 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@
},
"dependencies": {
"@qvac/decoder-audio": "^0.3.7",
"@qvac/diffusion-cpp": "^0.2.0",
"@qvac/dl-filesystem": "^0.2.1",
"@qvac/embed-llamacpp": "^0.13.4",
"@qvac/diffusion-cpp": "^0.3.0",
"@qvac/embed-llamacpp": "^0.14.0",
"@qvac/error": "^0.1.1",
"@qvac/langdetect-text": "^0.1.2",
"@qvac/llm-llamacpp": "^0.15.0",
"@qvac/llm-llamacpp": "^0.16.0",
"@qvac/logging": "^0.1.0",
"@qvac/ocr-onnx": "^0.4.2",
"@qvac/rag": "^0.4.4",
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/schemas/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export interface PluginModel {

export interface PluginModelResult {
model: PluginModel;
loader: unknown;
}

export interface PluginLogging {
Expand Down
4 changes: 1 addition & 3 deletions packages/sdk/server/bare/ops/load-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
ModelFileLocateFailedError,
} from "@/utils/errors-server";
import { getPlugin } from "@/server/plugins";
import type FilesystemDL from "@qvac/dl-filesystem";
import { promises as fsPromises } from "bare-fs";
import path from "bare-path";
import { getServerLogger } from "@/logging";
Expand Down Expand Up @@ -107,7 +106,7 @@ export async function loadModel(
modelConfig: modelConfig as Record<string, unknown>,
modelName,
artifacts,
}) as { model: AnyModel; loader: FilesystemDL };
}) as { model: AnyModel };

await result.model.load(false);

Expand All @@ -123,7 +122,6 @@ export async function loadModel(
config: modelConfig,
modelType: modelType as CanonicalModelType,
name: modelName,
loader: result.loader,
});

return modelInitializationTimeMs !== undefined
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk/server/bare/ops/unload-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export async function unloadModel(params: UnloadModelParams) {

clearFinetuneRuntimeState(modelId);

if (entry.local?.loader) {
await entry.local.loader.close();
}

if (entry.local?.model && entry.local.model.unload) {
await entry.local.model.unload();
}
Expand Down
40 changes: 16 additions & 24 deletions packages/sdk/server/bare/plugins/llamacpp-completion/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LlmLlamacpp, { type Loader as LlmLoader } from "@qvac/llm-llamacpp";
import LlmLlamacpp from "@qvac/llm-llamacpp";
import llmAddonLogging from "@qvac/llm-llamacpp/addonLogging";
import {
definePlugin,
Expand All @@ -21,9 +21,7 @@ import {
type LlmConfigInput,
} from "@/schemas";
import { createStreamLogger, registerAddonLogger } from "@/logging";
import { parseModelPath } from "@/server/utils";
import FilesystemDL from "@qvac/dl-filesystem";
import { asLoader } from "@/server/bare/utils/loader-adapter";
import { expandGGUFIntoShards } from "@/server/utils";
import { completion } from "@/server/bare/plugins/llamacpp-completion/ops/completion-stream";
import { finetune } from "@/server/bare/plugins/llamacpp-completion/ops/finetune";
import { translate } from "@/server/bare/ops/translate";
Expand Down Expand Up @@ -69,28 +67,22 @@ function createLlmModel(
llmConfig: LlmConfig,
projectionModelPath?: string,
) {
const { dirPath, basePath } = parseModelPath(modelPath);
const loader = new FilesystemDL({ dirPath });
const logger = createStreamLogger(modelId, ModelType.llamacppCompletion);
registerAddonLogger(modelId, ModelType.llamacppCompletion, logger);
const llmConfigStrings = transformLlmConfig(llmConfig);

const args = {
loader: asLoader<LlmLoader>(loader),
opts: { stats: true },
const modelFiles = expandGGUFIntoShards(modelPath);

const model = new LlmLlamacpp({
files: {
model: modelFiles,
...(projectionModelPath && { projectionModel: projectionModelPath }),
},
config: llmConfigStrings,
logger,
diskPath: dirPath,
modelName: basePath,
projectionModel: projectionModelPath
? parseModelPath(projectionModelPath).basePath
: "",
modelPath,
modelConfig: llmConfigStrings,
};

const model = new LlmLlamacpp(args, llmConfigStrings);

return { model, loader };
opts: { stats: true },
});

return { model };
}

export const llmPlugin = definePlugin({
Expand All @@ -116,14 +108,14 @@ export const llmPlugin = definePlugin({
createModel(params: CreateModelParams): PluginModelResult {
const llmConfig = (params.modelConfig ?? {}) as LlmConfig;

const { model, loader } = createLlmModel(
const { model } = createLlmModel(
params.modelId,
params.modelPath,
llmConfig,
params.artifacts?.["projectionModelPath"],
);

return { model, loader };
return { model };
},

handlers: {
Expand Down
32 changes: 11 additions & 21 deletions packages/sdk/server/bare/plugins/llamacpp-embedding/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import EmbedLlamacpp, {
type GGMLConfig,
type Loader as EmbedLoader,
} from "@qvac/embed-llamacpp";
import EmbedLlamacpp, { type GGMLConfig } from "@qvac/embed-llamacpp";
import embedAddonLogging from "@qvac/embed-llamacpp/addonLogging";
import {
definePlugin,
Expand All @@ -16,9 +13,7 @@ import {
type EmbedConfig,
} from "@/schemas";
import { createStreamLogger, registerAddonLogger } from "@/logging";
import { parseModelPath } from "@/server/utils";
import FilesystemDL from "@qvac/dl-filesystem";
import { asLoader } from "@/server/bare/utils/loader-adapter";
import { expandGGUFIntoShards } from "@/server/utils";
import { embed } from "@/server/bare/ops/embed";
import { forwardModelExecution } from "@/profiling/model-execution";

Expand Down Expand Up @@ -68,25 +63,20 @@ function createEmbeddingsModel(
modelPath: string,
embedConfig: EmbedConfig,
) {
const { dirPath, basePath } = parseModelPath(modelPath);
const loader = new FilesystemDL({ dirPath });
const logger = createStreamLogger(modelId, ModelType.llamacppEmbedding);
registerAddonLogger(modelId, ModelType.llamacppEmbedding, logger);

const config = transformEmbedConfig(embedConfig);
const modelFiles = expandGGUFIntoShards(modelPath);

const args = {
loader: asLoader<EmbedLoader>(loader),
opts: { stats: true },
const model = new EmbedLlamacpp({
files: { model: modelFiles },
config,
logger,
diskPath: dirPath,
modelName: basePath,
modelPath,
};

const model = new EmbedLlamacpp(args, config);
opts: { stats: true },
});

return { model, loader };
return { model };
}

export const embeddingsPlugin = definePlugin({
Expand All @@ -98,13 +88,13 @@ export const embeddingsPlugin = definePlugin({
createModel(params: CreateModelParams): PluginModelResult {
const embedConfig = (params.modelConfig ?? {}) as EmbedConfig;

const { model, loader } = createEmbeddingsModel(
const { model } = createEmbeddingsModel(
params.modelId,
params.modelPath,
embedConfig,
);

return { model, loader };
return { model };
},

handlers: {
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/server/bare/plugins/nmtcpp-translation/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
type ResolveResult,
} from "@/schemas";
import { createStreamLogger, registerAddonLogger } from "@/logging";
import { parseModelPath } from "@/server/utils";
import path from "bare-path";
import { ModelLoadFailedError } from "@/utils/errors-server";
import { translate } from "@/server/bare/ops/translate";
Expand Down Expand Up @@ -73,7 +72,8 @@ function deriveBergamotRegistryVocabSources(modelSrc: string) {
* Returns null if modelPath is not a recognisable Bergamot model.
*/
function deriveColocatedBergamotVocabPaths(modelPath: string) {
const { dirPath, basePath } = parseModelPath(modelPath);
const dirPath = path.dirname(modelPath);
const basePath = path.basename(modelPath);
const match = basePath.match(/^model\.([a-z]+)\.intgemm\.alphas\.bin$/);
if (!match?.[1]) return null;

Expand Down Expand Up @@ -162,7 +162,7 @@ function createNmtModel(
opts: { stats: true },
});

return { model, loader: null };
return { model };
}

async function resolveBergamotVocab(
Expand Down Expand Up @@ -286,7 +286,7 @@ export const nmtPlugin = definePlugin({
const pivotSrcVocabPath = artifacts["pivotSrcVocabPath"] ?? pivotDerived?.srcVocabPath;
const pivotDstVocabPath = artifacts["pivotDstVocabPath"] ?? pivotDerived?.dstVocabPath;

const { model, loader } = createNmtModel(
const { model } = createNmtModel(
params.modelId,
params.modelPath,
nmtConfig,
Expand All @@ -297,7 +297,7 @@ export const nmtPlugin = definePlugin({
pivotDstVocabPath,
);

return { model, loader };
return { model };
},

handlers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/server/bare/plugins/onnx-ocr/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const ocrPlugin = definePlugin({
ocrConfig,
);

return { model, loader: null };
return { model };
},

handlers: {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/server/bare/plugins/onnx-tts/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function createChatterboxModel(
opts: { stats: true },
exclusiveRun: true,
} as never);
return { model, loader: undefined };
return { model };
}

function createSupertonicModel(
Expand Down Expand Up @@ -250,7 +250,7 @@ function createSupertonicModel(
opts: { stats: true },
exclusiveRun: true,
} as never);
return { model, loader: undefined };
return { model };
}

export const ttsPlugin = definePlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import {
type ResolveResult,
} from "@/schemas";
import { createStreamLogger, registerAddonLogger } from "@/logging";
import { parseModelPath } from "@/server/utils";
import {
ModelLoadFailedError,
ParakeetArtifactsRequiredError,
} from "@/utils/errors-server";
import FilesystemDL from "@qvac/dl-filesystem";
import { transcribe } from "@/server/bare/ops/transcribe";
import { attachModelExecutionMs } from "@/profiling/model-execution";

Expand Down Expand Up @@ -169,8 +167,6 @@ function createParakeetModel(
);
}

const { dirPath } = parseModelPath(primaryPath);
const loader = new FilesystemDL({ dirPath });
const logger = createStreamLogger(params.modelId, ModelType.parakeetTranscription);
registerAddonLogger(params.modelId, ModelType.parakeetTranscription, logger);

Expand Down Expand Up @@ -198,7 +194,7 @@ function createParakeetModel(
logger,
});

return { model, loader };
return { model };
}

export const parakeetPlugin = definePlugin({
Expand Down
33 changes: 18 additions & 15 deletions packages/sdk/server/bare/plugins/sdcpp-generation/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import ImgStableDiffusion, { type ImgStableDiffusionArgs, type SdConfig } from "@qvac/diffusion-cpp";
import ImgStableDiffusion, {
type DiffusionFiles,
type SdConfig,
} from "@qvac/diffusion-cpp";
import addonLogging from "@qvac/diffusion-cpp/addonLogging";
import {
definePlugin,
Expand All @@ -15,7 +18,6 @@ import {
type SdcppConfig,
} from "@/schemas";
import { createStreamLogger, registerAddonLogger } from "@/logging";
import { parseModelPath } from "@/server/utils";
import { diffusion } from "./ops/diffusion";

type DiffusionArtifactKey =
Expand Down Expand Up @@ -72,25 +74,26 @@ export const diffusionPlugin = definePlugin({
createModel(params: CreateModelParams): PluginModelResult {
const { modelId, modelPath, modelConfig, artifacts } = params;
const config = (modelConfig ?? {}) as SdcppConfig;
const { dirPath, basePath } = parseModelPath(modelPath);
const logger = createStreamLogger(modelId, ModelType.sdcppGeneration);
registerAddonLogger(modelId, ModelType.sdcppGeneration, logger);

const addonArgs: ImgStableDiffusionArgs = {
diskPath: dirPath,
modelName: basePath,
logger,
opts: { stats: true },
...(artifacts?.["clipLModelPath"] && { clipLModel: artifacts["clipLModelPath"] }),
...(artifacts?.["clipGModelPath"] && { clipGModel: artifacts["clipGModelPath"] }),
...(artifacts?.["t5XxlModelPath"] && { t5XxlModel: artifacts["t5XxlModelPath"] }),
...(artifacts?.["llmModelPath"] && { llmModel: artifacts["llmModelPath"] }),
...(artifacts?.["vaeModelPath"] && { vaeModel: artifacts["vaeModelPath"] }),
const files: DiffusionFiles = {
model: modelPath,
...(artifacts?.["clipLModelPath"] && { clipL: artifacts["clipLModelPath"] }),
...(artifacts?.["clipGModelPath"] && { clipG: artifacts["clipGModelPath"] }),
...(artifacts?.["t5XxlModelPath"] && { t5Xxl: artifacts["t5XxlModelPath"] }),
...(artifacts?.["llmModelPath"] && { llm: artifacts["llmModelPath"] }),
...(artifacts?.["vaeModelPath"] && { vae: artifacts["vaeModelPath"] }),
};

const model = new ImgStableDiffusion(addonArgs, config as SdConfig);
const model = new ImgStableDiffusion({
files,
config: config as SdConfig,
logger,
opts: { stats: true },
});

return { model, loader: undefined };
return { model };
},

handlers: {
Expand Down
Loading
Loading