668faabfd2
- Added persistent settings in the Providers tab for ONNX execution provider and quantization/precision, replacing env-var-only configuration. - PI_TINY_DEVICE and PI_TINY_DTYPE env vars still override the matching setting at spawn time. - Added tinyWorkerEnvOverlay to map settings onto worker env without clobbering explicit env vars. - Updated docs and tests to reflect the new setting-first resolution order.
73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
normalizeTinyModelDevice,
|
|
resolveTinyModelDevicePreference,
|
|
TINY_MODEL_DEVICE_DEFAULT,
|
|
TINY_MODEL_DEVICE_SETTING_OPTIONS,
|
|
TINY_MODEL_DEVICE_SETTING_VALUES,
|
|
type TinyModelDevice,
|
|
tinyModelDeviceLoadOrder,
|
|
tinyModelDeviceSettingToEnv,
|
|
} from "../src/tiny/device";
|
|
|
|
function expectedDefaultDevice(): TinyModelDevice {
|
|
if (process.platform === "win32") return "dml";
|
|
if (process.platform === "linux" && process.arch === "x64") return "cuda";
|
|
return "cpu";
|
|
}
|
|
|
|
describe("tiny model device selection", () => {
|
|
it("defaults to the worker-safe accelerated provider for the platform", () => {
|
|
const preference = resolveTinyModelDevicePreference(undefined);
|
|
const expected = expectedDefaultDevice();
|
|
|
|
const expectedOrder: readonly TinyModelDevice[] = expected === "cpu" ? ["cpu"] : [expected, "cpu"];
|
|
expect(preference.device).toBe(expected);
|
|
expect(tinyModelDeviceLoadOrder(preference)).toEqual(expectedOrder);
|
|
});
|
|
|
|
it("accepts metal as a WebGPU alias without enabling unsafe macOS worker teardown", () => {
|
|
const expectedOrder: readonly TinyModelDevice[] = process.platform === "darwin" ? ["cpu"] : ["webgpu", "cpu"];
|
|
|
|
expect(normalizeTinyModelDevice("metal")).toBe("webgpu");
|
|
expect(tinyModelDeviceLoadOrder(resolveTinyModelDevicePreference("metal"))).toEqual(expectedOrder);
|
|
});
|
|
|
|
it("keeps explicit CPU runs CPU-only", () => {
|
|
const preference = resolveTinyModelDevicePreference(" cpu ");
|
|
|
|
expect(preference.device).toBe("cpu");
|
|
expect(tinyModelDeviceLoadOrder(preference)).toEqual(["cpu"]);
|
|
});
|
|
|
|
it("rejects unknown ONNX execution providers", () => {
|
|
expect(() => resolveTinyModelDevicePreference("neural-magic")).toThrow("Unsupported PI_TINY_DEVICE");
|
|
});
|
|
});
|
|
|
|
describe("tiny model device setting → PI_TINY_DEVICE mapping", () => {
|
|
it("returns undefined for the default sentinel so the worker keeps its platform default", () => {
|
|
expect(tinyModelDeviceSettingToEnv(TINY_MODEL_DEVICE_DEFAULT)).toBeUndefined();
|
|
expect(tinyModelDeviceSettingToEnv(undefined)).toBeUndefined();
|
|
expect(tinyModelDeviceSettingToEnv("")).toBeUndefined();
|
|
});
|
|
|
|
it("forwards a concrete device value verbatim for the worker to validate", () => {
|
|
expect(tinyModelDeviceSettingToEnv("metal")).toBe("metal");
|
|
expect(tinyModelDeviceSettingToEnv("cuda")).toBe("cuda");
|
|
});
|
|
|
|
it("keeps every non-default setting value resolvable by the worker", () => {
|
|
for (const value of TINY_MODEL_DEVICE_SETTING_VALUES) {
|
|
if (value === TINY_MODEL_DEVICE_DEFAULT) continue;
|
|
expect(() => normalizeTinyModelDevice(tinyModelDeviceSettingToEnv(value))).not.toThrow();
|
|
}
|
|
});
|
|
|
|
it("keeps submenu options aligned with the accepted values", () => {
|
|
expect(TINY_MODEL_DEVICE_SETTING_OPTIONS.map(option => option.value)).toEqual([
|
|
...TINY_MODEL_DEVICE_SETTING_VALUES,
|
|
]);
|
|
});
|
|
});
|