transcribe-cpp 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cuda.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Bring-your-own CUDA runtime preload — the TypeScript twin of the Python cu12
3
+ * provider's prepare() hook (bindings/python-native-cu12/__init__.py).
4
+ *
5
+ * The opt-in `@transcribe-cpp/<host>-cuda` package ships libtranscribe plus the
6
+ * ggml cuda/vulkan/cpu modules, but NOT the CUDA runtime (cudart/cublas) — the
7
+ * same posture the cu12 wheel takes, where the runtime comes from the
8
+ * nvidia-*-cu12 pip wheels. Node has no equivalent wheel ecosystem, so the user
9
+ * supplies the runtime and points TRANSCRIBE_CUDA_RUNTIME_DIR at it; we dlopen
10
+ * the libraries with RTLD_GLOBAL so the ggml-cuda module's DT_NEEDED sonames
11
+ * resolve against the already-loaded set when transcribe_init_backends loads it.
12
+ *
13
+ * Everything here is best-effort and silent: with no env var (or missing libs)
14
+ * this is a no-op. If a system CUDA toolkit is already on the default loader
15
+ * path the ggml-cuda module resolves cudart on its own; otherwise it quietly
16
+ * fails to load and the bundle's Vulkan/CPU modules keep working — the same
17
+ * degradation contract as a missing driver.
18
+ */
19
+ /**
20
+ * Preload the CUDA runtime from `TRANSCRIBE_CUDA_RUNTIME_DIR` (if set) so the
21
+ * ggml-cuda module can resolve cudart/cublas. Idempotent; safe to call when the
22
+ * selected provider has no cuda backend (it just won't find anything to load).
23
+ */
24
+ export declare function preloadCudaRuntime(): void;
package/dist/cuda.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Bring-your-own CUDA runtime preload — the TypeScript twin of the Python cu12
3
+ * provider's prepare() hook (bindings/python-native-cu12/__init__.py).
4
+ *
5
+ * The opt-in `@transcribe-cpp/<host>-cuda` package ships libtranscribe plus the
6
+ * ggml cuda/vulkan/cpu modules, but NOT the CUDA runtime (cudart/cublas) — the
7
+ * same posture the cu12 wheel takes, where the runtime comes from the
8
+ * nvidia-*-cu12 pip wheels. Node has no equivalent wheel ecosystem, so the user
9
+ * supplies the runtime and points TRANSCRIBE_CUDA_RUNTIME_DIR at it; we dlopen
10
+ * the libraries with RTLD_GLOBAL so the ggml-cuda module's DT_NEEDED sonames
11
+ * resolve against the already-loaded set when transcribe_init_backends loads it.
12
+ *
13
+ * Everything here is best-effort and silent: with no env var (or missing libs)
14
+ * this is a no-op. If a system CUDA toolkit is already on the default loader
15
+ * path the ggml-cuda module resolves cudart on its own; otherwise it quietly
16
+ * fails to load and the bundle's Vulkan/CPU modules keep working — the same
17
+ * degradation contract as a missing driver.
18
+ */
19
+ import * as fs from "node:fs";
20
+ import * as path from "node:path";
21
+ import koffi from "koffi";
22
+ // Runtime libraries the ggml-cuda module links, in dependency order (cublasLt
23
+ // before cublas). Each entry is [nvidia-pkg, subdir, soname]: the soname is the
24
+ // flat name to dlopen; the pkg/subdir mirror the nvidia-*-cu12 wheel layout so
25
+ // pointing TRANSCRIBE_CUDA_RUNTIME_DIR at a `site-packages/nvidia` tree also
26
+ // works. Linux ships versioned sonames under <pkg>/lib/; Windows ships DLLs
27
+ // under <pkg>/bin/ (the layout PyTorch relies on).
28
+ const RUNTIME_LIBS = process.platform === "win32"
29
+ ? [
30
+ ["cuda_runtime", "bin", "cudart64_12.dll"],
31
+ ["cublas", "bin", "cublasLt64_12.dll"],
32
+ ["cublas", "bin", "cublas64_12.dll"],
33
+ ]
34
+ : [
35
+ ["cuda_runtime", "lib", "libcudart.so.12"],
36
+ ["cublas", "lib", "libcublasLt.so.12"],
37
+ ["cublas", "lib", "libcublas.so.12"],
38
+ ];
39
+ // Keep loaded handles pinned for the process lifetime so the runtime never
40
+ // unloads out from under the ggml-cuda module.
41
+ const pinned = [];
42
+ let done = false;
43
+ /**
44
+ * Preload the CUDA runtime from `TRANSCRIBE_CUDA_RUNTIME_DIR` (if set) so the
45
+ * ggml-cuda module can resolve cudart/cublas. Idempotent; safe to call when the
46
+ * selected provider has no cuda backend (it just won't find anything to load).
47
+ */
48
+ export function preloadCudaRuntime() {
49
+ if (done)
50
+ return;
51
+ done = true;
52
+ const dir = process.env.TRANSCRIBE_CUDA_RUNTIME_DIR;
53
+ if (!dir)
54
+ return; // rely on a system toolkit already on the default loader path
55
+ for (const [pkg, sub, soname] of RUNTIME_LIBS) {
56
+ const candidates = [
57
+ path.join(dir, soname), // a flat directory of runtime libs
58
+ path.join(dir, pkg, sub, soname), // the nvidia-*-cu12 wheel layout
59
+ ];
60
+ const found = candidates.find((p) => fs.existsSync(p));
61
+ if (!found)
62
+ continue; // missing lib: degrade like a missing driver
63
+ try {
64
+ // RTLD_GLOBAL so the module's DT_NEEDED resolves against the loaded set;
65
+ // an absolute-path load is also the load-bearing step on Windows (the
66
+ // import table is satisfied by the already-loaded module, matched by name).
67
+ pinned.push(koffi.load(found, { global: true, lazy: false }));
68
+ }
69
+ catch {
70
+ // Same posture as a missing driver: never break the CPU/Vulkan path.
71
+ }
72
+ }
73
+ }
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * batch, streaming with committed/tentative text, typed per-family extensions,
6
6
  * backend discovery, log routing, and cooperative cancellation.
7
7
  */
8
- import { native, setLogHandler } from "./native.js";
8
+ import { native, setLogHandler, } from "./native.js";
9
9
  import { resolveLibrary } from "./loader.js";
10
10
  import * as g from "./_generated.js";
11
11
  import { Aborted, Busy, exceptionForStatus, InvalidArgument, ModelLoadError, NotImplementedByModel, OutputTruncated, TranscribeError, UnsupportedRequest, } from "./errors.js";
@@ -26,7 +26,10 @@ const KV_TYPES = {
26
26
  f32: g.TRANSCRIBE_KV_TYPE_F32,
27
27
  f16: g.TRANSCRIBE_KV_TYPE_F16,
28
28
  };
29
- const TASKS = { transcribe: g.TRANSCRIBE_TASK_TRANSCRIBE, translate: g.TRANSCRIBE_TASK_TRANSLATE };
29
+ const TASKS = {
30
+ transcribe: g.TRANSCRIBE_TASK_TRANSCRIBE,
31
+ translate: g.TRANSCRIBE_TASK_TRANSLATE,
32
+ };
30
33
  const TIMESTAMPS = {
31
34
  none: g.TRANSCRIBE_TIMESTAMPS_NONE,
32
35
  auto: g.TRANSCRIBE_TIMESTAMPS_AUTO,
@@ -146,7 +149,7 @@ function deferFree(lock, fn, after) {
146
149
  });
147
150
  }
148
151
  function callAsync(fn, ...args) {
149
- return new Promise((resolve, reject) => fn.async(...args, (err, res) => (err ? reject(err) : resolve(res))));
152
+ return new Promise((resolve, reject) => fn.async(...args, (err, res) => err ? reject(err) : resolve(res)));
150
153
  }
151
154
  // Coerce caller PCM to a Float32Array. A Float32Array is returned AS-IS (no
152
155
  // copy): the buffer is borrowed across the async native call, which reads it on
@@ -212,7 +215,11 @@ class Mutex {
212
215
  // is the value the binding *expects*, not one read from the loaded library.
213
216
  export function version() {
214
217
  const n = native();
215
- return { version: n.F.version(), commit: n.F.versionCommit(), headerHash: g.PUBLIC_HEADER_HASH };
218
+ return {
219
+ version: n.F.version(),
220
+ commit: n.F.versionCommit(),
221
+ headerHash: g.PUBLIC_HEADER_HASH,
222
+ };
216
223
  }
217
224
  export function libraryPath() {
218
225
  return native().libraryPath;
@@ -418,7 +425,11 @@ const FAMILY = {
418
425
  kind: g.TRANSCRIBE_EXT_KIND_PARAKEET_BUFFERED_STREAM,
419
426
  type: "transcribe_parakeet_buffered_stream_ext",
420
427
  init: "parakeetBufferedStreamExtInit",
421
- map: (o) => ({ left_ms: o.leftMs, chunk_ms: o.chunkMs, right_ms: o.rightMs }),
428
+ map: (o) => ({
429
+ left_ms: o.leftMs,
430
+ chunk_ms: o.chunkMs,
431
+ right_ms: o.rightMs,
432
+ }),
422
433
  },
423
434
  voxtral: {
424
435
  slot: "stream",
@@ -569,7 +580,8 @@ export class Session {
569
580
  this.#inFlight = null;
570
581
  cancel?.();
571
582
  }
572
- if (status === g.TRANSCRIBE_ERR_ABORTED || status === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
583
+ if (status === g.TRANSCRIBE_ERR_ABORTED ||
584
+ status === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
573
585
  const partial = {
574
586
  ...materialize(n, singleAccessors(n, h)),
575
587
  aborted: F.wasAborted(h),
@@ -582,7 +594,11 @@ export class Session {
582
594
  throw exc;
583
595
  }
584
596
  check(n, status, "transcribe_run");
585
- return { ...materialize(n, singleAccessors(n, h)), aborted: F.wasAborted(h), truncated: F.wasTruncated(h) };
597
+ return {
598
+ ...materialize(n, singleAccessors(n, h)),
599
+ aborted: F.wasAborted(h),
600
+ truncated: F.wasTruncated(h),
601
+ };
586
602
  });
587
603
  }
588
604
  #buildRunParams(opts) {
@@ -590,7 +606,10 @@ export class Session {
590
606
  const p = {};
591
607
  n.F.runParamsInit(p);
592
608
  p.task = lookup(TASKS, opts.task ?? "transcribe", "task");
593
- p.timestamps = lookup(TIMESTAMPS, opts.timestamps ?? "none", "timestamps");
609
+ // Default "auto" mirrors the C transcribe_run_params_init default:
610
+ // whisper resolves it to "segment" (its robust path), no-timestamp
611
+ // families resolve to "none".
612
+ p.timestamps = lookup(TIMESTAMPS, opts.timestamps ?? "auto", "timestamps");
594
613
  if (opts.language !== undefined)
595
614
  p.language = opts.language;
596
615
  if (opts.targetLanguage !== undefined)
@@ -643,13 +662,18 @@ export class Session {
643
662
  if (st === g.TRANSCRIBE_OK) {
644
663
  out.push({
645
664
  ok: true,
646
- result: { ...materialize(n, batchAccessors(n, h, i)), aborted: false, truncated: false },
665
+ result: {
666
+ ...materialize(n, batchAccessors(n, h, i)),
667
+ aborted: false,
668
+ truncated: false,
669
+ },
647
670
  });
648
671
  }
649
672
  else {
650
673
  const error = exceptionForStatus(st, F.statusString(st), `utterance ${i}`);
651
674
  error.utteranceIndex = i;
652
- if (st === g.TRANSCRIBE_ERR_ABORTED || st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
675
+ if (st === g.TRANSCRIBE_ERR_ABORTED ||
676
+ st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
653
677
  error.partialResult = {
654
678
  ...materialize(n, batchAccessors(n, h, i)),
655
679
  aborted: st === g.TRANSCRIBE_ERR_ABORTED,
package/dist/loader.d.ts CHANGED
@@ -4,7 +4,10 @@
4
4
  *
5
5
  * 1. TRANSCRIBE_LIBRARY — explicit path (developer escape hatch).
6
6
  * 2. A per-platform npm package `@transcribe-cpp/<tuple>` (the prebuilt
7
- * bundle wrapping the shared `transcribe-native-<tuple>` bytes).
7
+ * bundle wrapping the shared `transcribe-native-<tuple>` bytes). On a host
8
+ * with a CUDA build, the opt-in `@transcribe-cpp/<host>-cuda` package is
9
+ * preferred when installed (it is a strict superset — cuda + vulkan + cpu),
10
+ * else the default `<host>-cpu-vulkan` / `<host>-metal` package.
8
11
  * 3. A dev build tree (build-shared/src, build/src, …) found by walking up.
9
12
  *
10
13
  * Before dlopen of a platform package, its `contract.json` is validated
@@ -16,5 +19,11 @@ export interface Resolved {
16
19
  artifactDir: string;
17
20
  /** Provider package name, or null for env/dev resolution. */
18
21
  provider: string | null;
22
+ /**
23
+ * Backend kinds the resolved artifact advertises (from its contract.json),
24
+ * or empty for env/dev resolution where no contract is read. Drives the
25
+ * CUDA-runtime preload in native.ts.
26
+ */
27
+ backends: string[];
19
28
  }
20
29
  export declare function resolveLibrary(): Resolved;
package/dist/loader.js CHANGED
@@ -4,7 +4,10 @@
4
4
  *
5
5
  * 1. TRANSCRIBE_LIBRARY — explicit path (developer escape hatch).
6
6
  * 2. A per-platform npm package `@transcribe-cpp/<tuple>` (the prebuilt
7
- * bundle wrapping the shared `transcribe-native-<tuple>` bytes).
7
+ * bundle wrapping the shared `transcribe-native-<tuple>` bytes). On a host
8
+ * with a CUDA build, the opt-in `@transcribe-cpp/<host>-cuda` package is
9
+ * preferred when installed (it is a strict superset — cuda + vulkan + cpu),
10
+ * else the default `<host>-cpu-vulkan` / `<host>-metal` package.
8
11
  * 3. A dev build tree (build-shared/src, build/src, …) found by walking up.
9
12
  *
10
13
  * Before dlopen of a platform package, its `contract.json` is validated
@@ -48,7 +51,25 @@ function platformTuple() {
48
51
  }
49
52
  return null;
50
53
  }
51
- /** Validate a bundle's contract.json. Throws on a real mismatch. */
54
+ /**
55
+ * Candidate platform-package tuples for this host, most-preferred first. A
56
+ * CUDA-capable host lists its opt-in `<host>-cuda` tuple ahead of the default:
57
+ * the CUDA package is a strict superset (cuda + vulkan + cpu modules), so when a
58
+ * user has installed it we load it and let ggml pick CUDA at runtime — falling
59
+ * back to its bundled Vulkan/CPU when no driver is present. Hosts without a CUDA
60
+ * build just list their single default tuple.
61
+ */
62
+ function preferredTuples() {
63
+ const base = platformTuple();
64
+ if (!base)
65
+ return [];
66
+ const { platform, arch } = process;
67
+ if (arch === "x64" && (platform === "linux" || platform === "win32")) {
68
+ return [`${platform}-x64-cuda`, base];
69
+ }
70
+ return [base];
71
+ }
72
+ /** Validate a bundle's contract.json and return it. Throws on a real mismatch. */
52
73
  function validateContract(dir, provider) {
53
74
  const file = path.join(dir, "contract.json");
54
75
  let contract;
@@ -66,22 +87,30 @@ function validateContract(dir, provider) {
66
87
  throw new VersionMismatch(`provider ${provider} is version ${contract.version} but this binding is ${OUR_VERSION}. ` +
67
88
  `Pre-1.0 requires an exact base-version match.`);
68
89
  }
90
+ return contract;
69
91
  }
70
92
  function tryPlatformPackage() {
71
- const tuple = platformTuple();
72
- if (!tuple)
73
- return null;
74
- const provider = `@transcribe-cpp/${tuple}`;
75
93
  const require = createRequire(import.meta.url);
76
- let dir;
77
- try {
78
- dir = path.dirname(require.resolve(`${provider}/package.json`));
79
- }
80
- catch {
81
- return null; // package not installed — fall through to dev tree
94
+ // Most-preferred tuple first (CUDA ahead of the default on capable hosts).
95
+ for (const tuple of preferredTuples()) {
96
+ const provider = `@transcribe-cpp/${tuple}`;
97
+ let dir;
98
+ try {
99
+ dir = path.dirname(require.resolve(`${provider}/package.json`));
100
+ }
101
+ catch {
102
+ continue; // not installed — try the next candidate, then the dev tree
103
+ }
104
+ // A contract mismatch is fatal, not a fall-through.
105
+ const contract = validateContract(dir, provider);
106
+ return {
107
+ libraryPath: path.join(dir, LIB_NAME),
108
+ artifactDir: dir,
109
+ provider,
110
+ backends: contract.backends ?? [],
111
+ };
82
112
  }
83
- validateContract(dir, provider); // a contract mismatch is fatal, not a fall-through
84
- return { libraryPath: path.join(dir, LIB_NAME), artifactDir: dir, provider };
113
+ return null;
85
114
  }
86
115
  /** A library produced by the source build (`npm run build:native`). */
87
116
  function tryLocalPrebuild() {
@@ -93,7 +122,12 @@ function tryLocalPrebuild() {
93
122
  const dir = path.join(pkgRoot, "prebuilds", tuple, "lib");
94
123
  const cand = path.join(dir, LIB_NAME);
95
124
  return fs.existsSync(cand)
96
- ? { libraryPath: cand, artifactDir: dir, provider: `source-build:${tuple}` }
125
+ ? {
126
+ libraryPath: cand,
127
+ artifactDir: dir,
128
+ provider: `source-build:${tuple}`,
129
+ backends: [],
130
+ }
97
131
  : null;
98
132
  }
99
133
  /** Walk up to the repo root, then probe known build trees. */
@@ -116,7 +150,12 @@ function tryDevTree() {
116
150
  for (const rel of ["build-shared/src", "build-shared/bin", "build/src", "build/bin"]) {
117
151
  const cand = path.join(root, rel, LIB_NAME);
118
152
  if (fs.existsSync(cand)) {
119
- return { libraryPath: cand, artifactDir: path.dirname(cand), provider: null };
153
+ return {
154
+ libraryPath: cand,
155
+ artifactDir: path.dirname(cand),
156
+ provider: null,
157
+ backends: [],
158
+ };
120
159
  }
121
160
  }
122
161
  return null;
@@ -127,7 +166,12 @@ export function resolveLibrary() {
127
166
  if (!fs.existsSync(override)) {
128
167
  throw new TranscribeError(`TRANSCRIBE_LIBRARY points at a missing file: ${override}`);
129
168
  }
130
- return { libraryPath: override, artifactDir: path.dirname(override), provider: null };
169
+ return {
170
+ libraryPath: override,
171
+ artifactDir: path.dirname(override),
172
+ provider: null,
173
+ backends: [],
174
+ };
131
175
  }
132
176
  return (tryPlatformPackage() ??
133
177
  tryLocalPrebuild() ??
package/dist/native.js CHANGED
@@ -4,6 +4,7 @@
4
4
  * through. Lazy and memoized: nothing loads until the first call.
5
5
  */
6
6
  import { resolveLibrary } from "./loader.js";
7
+ import { preloadCudaRuntime } from "./cuda.js";
7
8
  import { abortProto, bindLibrary, logProto } from "./ffi.js";
8
9
  import { verifyLayouts } from "./abi.js";
9
10
  import { BackendError, VersionMismatch } from "./errors.js";
@@ -33,6 +34,12 @@ export function native() {
33
34
  if (cached)
34
35
  return cached;
35
36
  const resolved = resolveLibrary();
37
+ // Bring-your-own CUDA runtime: if the selected provider advertises cuda,
38
+ // preload cudart/cublas (the TS twin of the Python cu12 provider's prepare()
39
+ // hook) BEFORE any dlopen, so the ggml-cuda module's DT_NEEDED sonames resolve
40
+ // when initBackends loads it below. A no-op without cuda or the runtime dir.
41
+ if (resolved.backends.includes("cuda"))
42
+ preloadCudaRuntime();
36
43
  const bound = bindLibrary(resolved.libraryPath);
37
44
  verifyLayouts(bound);
38
45
  const nativeVersion = bound.F.version();
package/dist/types.d.ts CHANGED
@@ -109,6 +109,7 @@ export interface TranscribeOptions {
109
109
  task?: Task;
110
110
  language?: string;
111
111
  targetLanguage?: string;
112
+ /** Default "auto" (richest the model supports, per-family). */
112
113
  timestamps?: TimestampKind;
113
114
  keepSpecialTags?: boolean;
114
115
  /** Speculative-decode draft count; -1 = family default. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transcribe-cpp",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "TypeScript/Node.js bindings for transcribe.cpp — a C/C++ speech-to-text library built on ggml",
5
5
  "type": "module",
6
6
  "exports": {
@@ -51,11 +51,11 @@
51
51
  "koffi": "^3.0.2"
52
52
  },
53
53
  "optionalDependencies": {
54
- "@transcribe-cpp/darwin-arm64-metal": "0.0.8",
55
- "@transcribe-cpp/darwin-x64-cpu": "0.0.8",
56
- "@transcribe-cpp/linux-x64-cpu-vulkan": "0.0.8",
57
- "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.0.8",
58
- "@transcribe-cpp/win32-x64-cpu-vulkan": "0.0.8"
54
+ "@transcribe-cpp/darwin-arm64-metal": "0.0.10",
55
+ "@transcribe-cpp/darwin-x64-cpu": "0.0.10",
56
+ "@transcribe-cpp/linux-x64-cpu-vulkan": "0.0.10",
57
+ "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.0.10",
58
+ "@transcribe-cpp/win32-x64-cpu-vulkan": "0.0.10"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^22.0.0",