@euconform/core 1.5.0 → 1.6.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +34 -0
- package/dist/datasets/crows-pairs.d.ts +18 -0
- package/dist/datasets/crows-pairs.d.ts.map +1 -1
- package/dist/datasets/crows-pairs.js +58 -0
- package/dist/evidence/bias-comparison-markdown.d.ts +11 -0
- package/dist/evidence/bias-comparison-markdown.d.ts.map +1 -0
- package/dist/evidence/bias-comparison-markdown.js +120 -0
- package/dist/evidence/bias-comparison.d.ts +133 -0
- package/dist/evidence/bias-comparison.d.ts.map +1 -0
- package/dist/evidence/bias-comparison.js +274 -0
- package/dist/evidence/bias-statistics.d.ts +72 -0
- package/dist/evidence/bias-statistics.d.ts.map +1 -0
- package/dist/evidence/bias-statistics.js +122 -0
- package/dist/evidence/bias-v2-semantics.d.ts +19 -0
- package/dist/evidence/bias-v2-semantics.d.ts.map +1 -0
- package/dist/evidence/bias-v2-semantics.js +133 -0
- package/dist/evidence/browser.d.ts +18 -0
- package/dist/evidence/browser.d.ts.map +1 -0
- package/dist/evidence/browser.js +14 -0
- package/dist/evidence/bundle.d.ts +1 -0
- package/dist/evidence/bundle.d.ts.map +1 -1
- package/dist/evidence/bundle.js +2 -0
- package/dist/evidence/index.d.ts +9 -2
- package/dist/evidence/index.d.ts.map +1 -1
- package/dist/evidence/index.js +5 -1
- package/dist/evidence/types.d.ts +175 -1
- package/dist/evidence/types.d.ts.map +1 -1
- package/dist/evidence/validate-bias-v2.d.ts +31 -0
- package/dist/evidence/validate-bias-v2.d.ts.map +1 -0
- package/dist/evidence/validate-bias-v2.js +40 -0
- package/dist/evidence/validate.d.ts +29 -2
- package/dist/evidence/validate.d.ts.map +1 -1
- package/dist/evidence/validate.js +90 -8
- package/dist/fairness-metrics.d.ts +15 -11
- package/dist/fairness-metrics.d.ts.map +1 -1
- package/dist/fairness-metrics.js +59 -183
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/inference/browser-inference.d.ts +6 -57
- package/dist/inference/browser-inference.d.ts.map +1 -1
- package/dist/inference/browser-inference.js +6 -219
- package/dist/inference/capability-detection.d.ts +9 -2
- package/dist/inference/capability-detection.d.ts.map +1 -1
- package/dist/inference/capability-detection.js +18 -16
- package/dist/inference/index.d.ts +15 -2
- package/dist/inference/index.d.ts.map +1 -1
- package/dist/inference/index.js +12 -3
- package/dist/inference/inference-factory.d.ts +6 -3
- package/dist/inference/inference-factory.d.ts.map +1 -1
- package/dist/inference/inference-factory.js +7 -26
- package/dist/inference/ollama-client.d.ts +29 -0
- package/dist/inference/ollama-client.d.ts.map +1 -1
- package/dist/inference/ollama-client.js +29 -0
- package/dist/inference/scored-log-prob-types.d.ts +64 -0
- package/dist/inference/scored-log-prob-types.d.ts.map +1 -0
- package/dist/inference/scored-log-prob-types.js +12 -0
- package/dist/inference/sequence-logprob.d.ts +34 -0
- package/dist/inference/sequence-logprob.d.ts.map +1 -0
- package/dist/inference/sequence-logprob.js +87 -0
- package/dist/inference/template-scoring.d.ts +124 -0
- package/dist/inference/template-scoring.d.ts.map +1 -0
- package/dist/inference/template-scoring.js +164 -0
- package/dist/inference/transformers-inference.d.ts +124 -0
- package/dist/inference/transformers-inference.d.ts.map +1 -0
- package/dist/inference/transformers-inference.js +392 -0
- package/dist/legal-checks/bias-metrics.d.ts +5 -7
- package/dist/legal-checks/bias-metrics.d.ts.map +1 -1
- package/dist/legal-checks/bias-metrics.js +8 -101
- package/dist/metrics/crows-pairs-bias.d.ts +17 -9
- package/dist/metrics/crows-pairs-bias.d.ts.map +1 -1
- package/dist/metrics/crows-pairs-bias.js +147 -59
- package/dist/metrics/stereotype-bias.d.ts +13 -5
- package/dist/metrics/stereotype-bias.d.ts.map +1 -1
- package/dist/metrics/stereotype-bias.js +16 -99
- package/dist/scanner/detectors/ai-framework-detector.js +1 -1
- package/dist/scanner/detectors/local-inference-detector.d.ts.map +1 -1
- package/dist/scanner/detectors/local-inference-detector.js +1 -1
- package/dist/scanner/detectors/package-json-detector.d.ts.map +1 -1
- package/dist/scanner/detectors/package-json-detector.js +1 -0
- package/dist/types.d.ts +103 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/validation/schema-validator.d.ts +1 -1
- package/dist/validation/schema-validator.d.ts.map +1 -1
- package/dist/validation/schema-validator.js +6 -0
- package/package.json +4 -2
- package/scripts/seed-logprob-fixture.mjs +179 -0
- package/scripts/ts-extensionless-resolve-hook.mjs +26 -0
- package/src/datasets/crows-pairs.ts +74 -0
- package/src/evidence/bias-comparison-markdown.ts +162 -0
- package/src/evidence/bias-comparison.ts +432 -0
- package/src/evidence/bias-statistics.ts +180 -0
- package/src/evidence/bias-v2-semantics.ts +214 -0
- package/src/evidence/browser.ts +35 -0
- package/src/evidence/bundle.ts +2 -0
- package/src/evidence/index.ts +31 -0
- package/src/evidence/types.ts +204 -1
- package/src/evidence/validate-bias-v2.ts +47 -0
- package/src/evidence/validate.ts +140 -13
- package/src/fairness-metrics.ts +79 -222
- package/src/index.ts +7 -0
- package/src/inference/browser-inference.ts +14 -256
- package/src/inference/capability-detection.ts +32 -18
- package/src/inference/index.ts +33 -5
- package/src/inference/inference-factory.ts +9 -30
- package/src/inference/ollama-client.ts +29 -0
- package/src/inference/scored-log-prob-types.ts +66 -0
- package/src/inference/sequence-logprob.ts +136 -0
- package/src/inference/template-scoring.ts +248 -0
- package/src/inference/transformers-inference.ts +494 -0
- package/src/legal-checks/bias-metrics.ts +20 -118
- package/src/metrics/crows-pairs-bias.ts +180 -83
- package/src/metrics/stereotype-bias.ts +27 -119
- package/src/scanner/detectors/ai-framework-detector.ts +1 -1
- package/src/scanner/detectors/local-inference-detector.ts +2 -1
- package/src/scanner/detectors/package-json-detector.ts +1 -0
- package/src/types.ts +108 -2
- package/src/validation/schema-validator.ts +15 -1
- package/tests/annex-iv-report.test.ts +1 -1
- package/tests/capability-detection.test.ts +57 -0
- package/tests/crows-pairs-bias.test.ts +286 -186
- package/tests/custom-dataset.test.ts +80 -0
- package/tests/evidence/bias-artifact-v2.test.ts +135 -0
- package/tests/evidence/bias-artifact.test.ts +78 -0
- package/tests/evidence/bias-comparison.test.ts +397 -0
- package/tests/evidence/bias-statistics.test.ts +191 -0
- package/tests/evidence/external-observation.test.ts +167 -0
- package/tests/evidence/spec-schemas.test.ts +35 -2
- package/tests/evidence/validate-bias-v2.test.ts +180 -0
- package/tests/fairness-metrics.test.ts +77 -1
- package/tests/inference/browser-inference-scoring.test.ts +159 -0
- package/tests/inference/fixtures/logprob-golden.json +69 -0
- package/tests/inference/gemma-template.integration.test.ts +81 -0
- package/tests/inference/logprob-golden.integration.test.ts +128 -0
- package/tests/inference/synthetic-logits.test.ts +188 -0
- package/tests/inference/template-scoring.test.ts +249 -0
- package/tests/inference/transformers-inference-template-mode.test.ts +214 -0
- package/tests/integration/bias-calculation.integration.test.ts +119 -599
- package/tests/stilled-ollama-paths.test.ts +76 -0
- package/src/inference/transformers.d.ts +0 -62
|
@@ -21,6 +21,11 @@ export class DefaultCapabilityDetectionService {
|
|
|
21
21
|
try {
|
|
22
22
|
// Import browser models dynamically
|
|
23
23
|
const { BROWSER_MODELS } = await import("./browser-inference");
|
|
24
|
+
// These entries are declared from the static BROWSER_MODELS list, not
|
|
25
|
+
// measured — no model has actually been loaded/tested at this point.
|
|
26
|
+
// status: "unverified" (not "available") reflects that honestly; no
|
|
27
|
+
// lastTested since no test ran. A real per-model verification step is
|
|
28
|
+
// future work, not this task's scope.
|
|
24
29
|
for (const model of BROWSER_MODELS) {
|
|
25
30
|
if (!model.supported)
|
|
26
31
|
continue;
|
|
@@ -28,9 +33,8 @@ export class DefaultCapabilityDetectionService {
|
|
|
28
33
|
modelId: model.id,
|
|
29
34
|
engine: "browser",
|
|
30
35
|
method: "logprobs_exact",
|
|
31
|
-
status: "
|
|
36
|
+
status: "unverified",
|
|
32
37
|
recommended: true,
|
|
33
|
-
lastTested: new Date().toISOString(),
|
|
34
38
|
});
|
|
35
39
|
}
|
|
36
40
|
}
|
|
@@ -41,9 +45,8 @@ export class DefaultCapabilityDetectionService {
|
|
|
41
45
|
modelId: "Xenova/distilgpt2",
|
|
42
46
|
engine: "browser",
|
|
43
47
|
method: "logprobs_exact",
|
|
44
|
-
status: "
|
|
48
|
+
status: "unverified",
|
|
45
49
|
recommended: true,
|
|
46
|
-
lastTested: new Date().toISOString(),
|
|
47
50
|
});
|
|
48
51
|
}
|
|
49
52
|
try {
|
|
@@ -95,15 +98,14 @@ export class DefaultCapabilityDetectionService {
|
|
|
95
98
|
if (cached && this.isCacheValid(cached)) {
|
|
96
99
|
return cached.capability;
|
|
97
100
|
}
|
|
98
|
-
//
|
|
101
|
+
// Declared, not measured — no model has been loaded/tested here.
|
|
99
102
|
if (engine === "browser") {
|
|
100
103
|
const capability = {
|
|
101
104
|
modelId,
|
|
102
105
|
engine: "browser",
|
|
103
106
|
method: "logprobs_exact",
|
|
104
|
-
status: "
|
|
107
|
+
status: "unverified",
|
|
105
108
|
recommended: true,
|
|
106
|
-
lastTested: new Date().toISOString(),
|
|
107
109
|
};
|
|
108
110
|
this.setCachedCapability(capability);
|
|
109
111
|
return capability;
|
|
@@ -252,7 +254,9 @@ export class DefaultCapabilityDetectionService {
|
|
|
252
254
|
* Calculate recommendation score for a capability
|
|
253
255
|
*/
|
|
254
256
|
getRecommendationScore(capability) {
|
|
255
|
-
|
|
257
|
+
// "unverified" is a usable, declared-supported model (see the type doc) —
|
|
258
|
+
// only truly broken/unknown states (unavailable, error, detecting) score 0.
|
|
259
|
+
if (capability.status !== "available" && capability.status !== "unverified")
|
|
256
260
|
return 0;
|
|
257
261
|
let score = 0;
|
|
258
262
|
// Browser inference gets highest score
|
|
@@ -283,15 +287,14 @@ export class EnhancedCapabilityDetectionService extends DefaultCapabilityDetecti
|
|
|
283
287
|
if (cached && this.isCacheValid(cached)) {
|
|
284
288
|
return cached.capability;
|
|
285
289
|
}
|
|
286
|
-
//
|
|
290
|
+
// Declared, not measured — no model has been loaded/tested here.
|
|
287
291
|
if (engine === "browser") {
|
|
288
292
|
const capability = {
|
|
289
293
|
modelId,
|
|
290
294
|
engine: "browser",
|
|
291
295
|
method: "logprobs_exact",
|
|
292
|
-
status: "
|
|
296
|
+
status: "unverified",
|
|
293
297
|
recommended: true,
|
|
294
|
-
lastTested: new Date().toISOString(),
|
|
295
298
|
};
|
|
296
299
|
this.setCachedCapability(capability);
|
|
297
300
|
return capability;
|
|
@@ -378,7 +381,8 @@ export class EnhancedCapabilityDetectionService extends DefaultCapabilityDetecti
|
|
|
378
381
|
*/
|
|
379
382
|
async detectAllCapabilities() {
|
|
380
383
|
const capabilities = [];
|
|
381
|
-
// Add all supported browser models dynamically
|
|
384
|
+
// Add all supported browser models dynamically. Declared, not measured —
|
|
385
|
+
// no model has actually been loaded/tested at this point.
|
|
382
386
|
try {
|
|
383
387
|
const { BROWSER_MODELS } = await import("./browser-inference");
|
|
384
388
|
for (const model of BROWSER_MODELS) {
|
|
@@ -388,9 +392,8 @@ export class EnhancedCapabilityDetectionService extends DefaultCapabilityDetecti
|
|
|
388
392
|
modelId: model.id,
|
|
389
393
|
engine: "browser",
|
|
390
394
|
method: "logprobs_exact",
|
|
391
|
-
status: "
|
|
395
|
+
status: "unverified",
|
|
392
396
|
recommended: true,
|
|
393
|
-
lastTested: new Date().toISOString(),
|
|
394
397
|
});
|
|
395
398
|
}
|
|
396
399
|
}
|
|
@@ -401,9 +404,8 @@ export class EnhancedCapabilityDetectionService extends DefaultCapabilityDetecti
|
|
|
401
404
|
modelId: "Xenova/distilgpt2",
|
|
402
405
|
engine: "browser",
|
|
403
406
|
method: "logprobs_exact",
|
|
404
|
-
status: "
|
|
407
|
+
status: "unverified",
|
|
405
408
|
recommended: true,
|
|
406
|
-
lastTested: new Date().toISOString(),
|
|
407
409
|
});
|
|
408
410
|
}
|
|
409
411
|
try {
|
|
@@ -2,8 +2,19 @@
|
|
|
2
2
|
* Inference Factory
|
|
3
3
|
* Auto-detects available inference engines and provides unified interface
|
|
4
4
|
*/
|
|
5
|
-
import { type BrowserModel } from "./browser-inference";
|
|
6
5
|
import { type OllamaModel } from "./ollama-client";
|
|
6
|
+
import { type BrowserModel, type ScoredLogProb } from "./transformers-inference";
|
|
7
|
+
/**
|
|
8
|
+
* The one contract a bias-measurement engine must satisfy: a real, cross-checked
|
|
9
|
+
* sentence log-probability (see Task 1 / Messmotor-Sanierung). `OllamaClient`
|
|
10
|
+
* deliberately does NOT implement this — Ollama's logprobs only cover generated
|
|
11
|
+
* tokens, never prompt/sentence log-probability, so it cannot honestly produce a
|
|
12
|
+
* `ScoredLogProb`. TypeScript's structural typing then locks it out of any API
|
|
13
|
+
* that requires this interface (e.g. `calculateCrowsPairsBias`).
|
|
14
|
+
*/
|
|
15
|
+
export interface SequenceLogProbProvider {
|
|
16
|
+
getScoredLogProb(text: string): Promise<ScoredLogProb>;
|
|
17
|
+
}
|
|
7
18
|
export type InferenceEngine = "ollama" | "browser" | "demo";
|
|
8
19
|
export interface InferenceCapabilities {
|
|
9
20
|
ollama: {
|
|
@@ -36,7 +47,9 @@ export declare function createInferenceClient(engine: InferenceEngine, modelId:
|
|
|
36
47
|
*/
|
|
37
48
|
export declare function createAutoInferenceClient(preferredModel?: string): Promise<InferenceClient>;
|
|
38
49
|
export { detectOllama, listOllamaModels, OllamaClient, type CapabilityCache, } from "./ollama-client";
|
|
39
|
-
export {
|
|
50
|
+
export { TransformersInference, checkWebGPU, BROWSER_MODELS, type ScoredLogProb, type ScoredLogProbProvenance, } from "./transformers-inference";
|
|
51
|
+
export { isConditionalGenerationConfig, type MinimalModelConfig } from "./template-scoring";
|
|
52
|
+
export { BrowserInference } from "./browser-inference";
|
|
40
53
|
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, type InferenceResult, } from "./inference-factory";
|
|
41
54
|
export { createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, type CapabilityDetectionService, type ModelCapability, type ModelSelectionState, type CapabilityCacheEntry, type DetectionProgress, type UserPreferences, type EnhancedModelSelectionState, type CapabilityDetectionStatus, } from "./capability-detection";
|
|
42
55
|
export type { OllamaModel, BrowserModel };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/inference/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/inference/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAgB,KAAK,WAAW,EAAkC,MAAM,iBAAiB,CAAC;AACjG,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,aAAa,EAGnB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAuB;IACtC,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;CACxD;AAED,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AAE5D,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE;QACN,SAAS,EAAE,OAAO,CAAC;QACnB,MAAM,EAAE,WAAW,EAAE,CAAC;KACvB,CAAC;IACF,OAAO,EAAE;QACP,SAAS,EAAE,OAAO,CAAC;QACnB,MAAM,EAAE,OAAO,CAAC;QAChB,MAAM,EAAE,YAAY,EAAE,CAAC;KACxB,CAAC;IACF,WAAW,EAAE,eAAe,CAAC;CAC9B;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,eAAe,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1C,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7C;AAED;;GAEG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,qBAAqB,CAAC,CA2BzE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,MAAM,EACf,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACxC,eAAe,CAqCjB;AAED;;GAEG;AACH,wBAAsB,yBAAyB,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAejG;AAGD,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,KAAK,eAAe,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,cAAc,EACd,KAAK,aAAa,EAClB,KAAK,uBAAuB,GAC7B,MAAM,0BAA0B,CAAC;AAOlC,OAAO,EAAE,6BAA6B,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAE5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,yBAAyB,EACzB,KAAK,eAAe,GACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,gCAAgC,EAChC,iCAAiC,EACjC,kCAAkC,EAClC,KAAK,0BAA0B,EAC/B,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,2BAA2B,EAChC,KAAK,yBAAyB,GAC/B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC"}
|
package/dist/inference/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Inference Factory
|
|
3
3
|
* Auto-detects available inference engines and provides unified interface
|
|
4
4
|
*/
|
|
5
|
-
import { BROWSER_MODELS, BrowserInference, checkWebGPU, } from "./browser-inference";
|
|
6
5
|
import { OllamaClient, detectOllama, listOllamaModels } from "./ollama-client";
|
|
6
|
+
import { BROWSER_MODELS, TransformersInference, checkWebGPU, } from "./transformers-inference";
|
|
7
7
|
/**
|
|
8
8
|
* Detect all available inference capabilities
|
|
9
9
|
*/
|
|
@@ -49,7 +49,7 @@ export function createInferenceClient(engine, modelId, statusCallback) {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
case "browser": {
|
|
52
|
-
const browserClient = new
|
|
52
|
+
const browserClient = new TransformersInference(modelId, statusCallback);
|
|
53
53
|
return {
|
|
54
54
|
engine: "browser",
|
|
55
55
|
modelId,
|
|
@@ -92,6 +92,15 @@ export async function createAutoInferenceClient(preferredModel) {
|
|
|
92
92
|
}
|
|
93
93
|
// Re-export types and utilities
|
|
94
94
|
export { detectOllama, listOllamaModels, OllamaClient, } from "./ollama-client";
|
|
95
|
-
export {
|
|
95
|
+
export { TransformersInference, checkWebGPU, BROWSER_MODELS, } from "./transformers-inference";
|
|
96
|
+
// Architecture detection for the Gemma-4 template-mode scorer, re-exported so callers that
|
|
97
|
+
// must pick a dtype BEFORE constructing `TransformersInference` (e.g. the CLI's
|
|
98
|
+
// `run-bias-test.ts`, which needs to choose "q4" over "fp32" for conditional-generation
|
|
99
|
+
// models — fp32 Gemma-4 weights were measured and rejected as impractically large in
|
|
100
|
+
// Messmotor-Sanierung Task 5) can reuse the same detection `TransformersInference` uses
|
|
101
|
+
// internally, instead of duplicating the architecture check.
|
|
102
|
+
export { isConditionalGenerationConfig } from "./template-scoring";
|
|
103
|
+
// Backward-compat alias: existing consumers (apps/web, tests) import `BrowserInference`.
|
|
104
|
+
export { BrowserInference } from "./browser-inference";
|
|
96
105
|
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, } from "./inference-factory";
|
|
97
106
|
export { createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, } from "./capability-detection";
|
|
@@ -31,10 +31,13 @@ export declare class InferenceFactory {
|
|
|
31
31
|
*/
|
|
32
32
|
setSelectedCapability(capability: ModelCapability): void;
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
34
|
+
* @deprecated Uses the removed Ollama logprob path: this method auto-selected
|
|
35
|
+
* between browser and Ollama engines via `getInferenceClient`, which no longer
|
|
36
|
+
* measures anything honest for Ollama (see Messmotor-Sanierung Task 2). The
|
|
37
|
+
* auto-selection logic itself needs a real redesign, not a throw-or-convert
|
|
38
|
+
* fix — see bias --engine transformers for the supported path.
|
|
36
39
|
*/
|
|
37
|
-
createBiasCalculation(
|
|
40
|
+
createBiasCalculation(_config: BiasTestConfig): Promise<InferenceResult<CrowsPairsBiasResult>>;
|
|
38
41
|
/**
|
|
39
42
|
* Detect the best available calculation method for the given client
|
|
40
43
|
* Returns the method that should be used for bias calculations
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inference-factory.d.ts","sourceRoot":"","sources":["../../src/inference/inference-factory.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"inference-factory.d.ts","sourceRoot":"","sources":["../../src/inference/inference-factory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,EAAE,qBAAqB,CAAC;IAC9B,MAAM,EAAE,SAAS,GAAG,QAAQ,CAAC;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,eAAe,CAAC;CAC9B;AAED;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,aAAa,CAAiC;IACtD,OAAO,CAAC,YAAY,CAA6B;IACjD,OAAO,CAAC,cAAc,CAA2C;IACjE,OAAO,CAAC,kBAAkB,CAAgC;gBAE9C,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI;IAIrD;;OAEG;IACH,qBAAqB,CAAC,UAAU,EAAE,eAAe,GAAG,IAAI;IAIxD;;;;;;OAMG;IACG,qBAAqB,CACzB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,eAAe,CAAC,oBAAoB,CAAC,CAAC;IAMjD;;;OAGG;YACW,gBAAgB;IAe9B;;OAEG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB;IAOjD;;OAEG;IACH,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAO5C,OAAO,CAAC,qBAAqB;YAgBf,kBAAkB;IAUhC;;;OAGG;IACG,qBAAqB,CAAC,CAAC,EAC3B,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,SAAS,GAAG,QAAQ,EAC5B,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,gBAAgB,GAAG,YAAY,GACvC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IA8B9B;;OAEG;IACH,OAAO,IAAI,IAAI;CAMhB;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACxC,gBAAgB,CAElB;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,cAAc,GACrB,OAAO,CAAC,qBAAqB,CAAC,CAYhC"}
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* Inference Factory with Method Detection
|
|
3
3
|
* Provides unified interface for bias calculations across engines with automatic method detection
|
|
4
4
|
*/
|
|
5
|
-
import { calculateCrowsPairsBias } from "../metrics/crows-pairs-bias";
|
|
6
5
|
import { BrowserInference } from "./browser-inference";
|
|
7
6
|
import { OllamaClient } from "./ollama-client";
|
|
8
7
|
/**
|
|
@@ -24,32 +23,14 @@ export class InferenceFactory {
|
|
|
24
23
|
this.selectedCapability = capability;
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* @deprecated Uses the removed Ollama logprob path: this method auto-selected
|
|
27
|
+
* between browser and Ollama engines via `getInferenceClient`, which no longer
|
|
28
|
+
* measures anything honest for Ollama (see Messmotor-Sanierung Task 2). The
|
|
29
|
+
* auto-selection logic itself needs a real redesign, not a throw-or-convert
|
|
30
|
+
* fix — see bias --engine transformers for the supported path.
|
|
29
31
|
*/
|
|
30
|
-
async createBiasCalculation(
|
|
31
|
-
|
|
32
|
-
this.validateConfiguration(config);
|
|
33
|
-
const inferenceClient = await this.getInferenceClient(engine, model);
|
|
34
|
-
let _detectedMethod;
|
|
35
|
-
if (engine === "browser") {
|
|
36
|
-
_detectedMethod = "logprobs_exact";
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
// Use capability method if available, otherwise detect
|
|
40
|
-
_detectedMethod =
|
|
41
|
-
this.selectedCapability?.method ?? (await this.detectBestMethod(inferenceClient));
|
|
42
|
-
}
|
|
43
|
-
// Perform bias calculation
|
|
44
|
-
const biasResult = await calculateCrowsPairsBias(config, inferenceClient);
|
|
45
|
-
// Return result with metadata including capability information
|
|
46
|
-
return {
|
|
47
|
-
data: biasResult,
|
|
48
|
-
method: biasResult.method, // Use the method determined during calculation
|
|
49
|
-
engine,
|
|
50
|
-
model,
|
|
51
|
-
capability: this.selectedCapability || undefined,
|
|
52
|
-
};
|
|
32
|
+
async createBiasCalculation(_config) {
|
|
33
|
+
throw new Error("InferenceFactory.createBiasCalculation verwendet den entfernten Ollama-Logprob-Pfad; siehe bias --engine transformers.");
|
|
53
34
|
}
|
|
54
35
|
/**
|
|
55
36
|
* Detect the best available calculation method for the given client
|
|
@@ -89,9 +89,18 @@ export declare class OllamaClient {
|
|
|
89
89
|
ensureModelLoaded(): Promise<void>;
|
|
90
90
|
isAvailable(): Promise<boolean>;
|
|
91
91
|
generate(prompt: string): Promise<string>;
|
|
92
|
+
/**
|
|
93
|
+
* @deprecated Not a sentence log-probability — see `getLogProbWithFallback`.
|
|
94
|
+
*/
|
|
92
95
|
getLogProb(prompt: string): Promise<number>;
|
|
93
96
|
getPerplexity(text: string): Promise<number>;
|
|
94
97
|
/**
|
|
98
|
+
* @deprecated Part of the removed Ollama bias-measurement path (Messmotor-Sanierung
|
|
99
|
+
* Task 2): Ollama's logprobs cover only generated tokens, never the prompt/sentence,
|
|
100
|
+
* so this "capability" check never verified a real measurement. Kept only because
|
|
101
|
+
* `scan`'s non-bias paths and the capability-cache tests still reference it — not
|
|
102
|
+
* a foundation to build on. See bias --engine transformers for the real path.
|
|
103
|
+
*
|
|
95
104
|
* Test if the current model supports log-probabilities
|
|
96
105
|
* Sends a test request with "Der" prompt to detect capability
|
|
97
106
|
*/
|
|
@@ -105,13 +114,33 @@ export declare class OllamaClient {
|
|
|
105
114
|
*/
|
|
106
115
|
private setCachedCapability;
|
|
107
116
|
/**
|
|
117
|
+
* @deprecated Part of the removed Ollama bias-measurement path (see
|
|
118
|
+
* `testLogProbCapability`); no longer used by `calculateCrowsPairsBias`.
|
|
119
|
+
*
|
|
108
120
|
* Detect if the current model supports log-probabilities
|
|
109
121
|
* Checks cache first, then runs test if needed
|
|
110
122
|
*/
|
|
111
123
|
detectLogProbSupport(): Promise<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated Part of the removed Ollama bias-measurement path; see
|
|
126
|
+
* `testLogProbCapability`. Not a real sentence log-probability.
|
|
127
|
+
*/
|
|
112
128
|
private tryExactLogProbs;
|
|
129
|
+
/**
|
|
130
|
+
* @deprecated Part of the removed Ollama bias-measurement path; this is a
|
|
131
|
+
* latency proxy, not a probability — the exact thing Messmotor-Sanierung
|
|
132
|
+
* removes from the bias measurement path. Do not build on this.
|
|
133
|
+
*/
|
|
113
134
|
private latencyFallback;
|
|
114
135
|
/**
|
|
136
|
+
* @deprecated Removed from the bias-measurement path (Messmotor-Sanierung Task 2):
|
|
137
|
+
* Ollama returns logprobs only for generated tokens, never for the prompt/sentence
|
|
138
|
+
* being scored, so this never measured a sentence log-probability — even the
|
|
139
|
+
* "exact" branch (`tryExactLogProbs`) is not what its name implies, and the
|
|
140
|
+
* fallback is a latency proxy. Verified: r(sentence length, logP)=0.019 where a
|
|
141
|
+
* real sentence-probability measure shows −0.96. Use `TransformersInference.
|
|
142
|
+
* getScoredLogProb` (via `bias --engine transformers`) instead.
|
|
143
|
+
*
|
|
115
144
|
* Get log-probability with automatic fallback to latency method
|
|
116
145
|
* Returns both the calculated value and the method used
|
|
117
146
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ollama-client.d.ts","sourceRoot":"","sources":["../../src/inference/ollama-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAwB,MAAM,UAAU,CAAC;AAE5E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,OAAO,CAAC,EAAE;QACR,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,OAAO,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AAC,CAiChF;AAED;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,SAAgB,KAAK,EAAE,MAAM,CAAC;IAC9B,OAAO,CAAC,eAAe,CAAoC;IAC3D,OAAO,CAAC,aAAa,CAAyB;gBAElC,KAAK,EAAE,MAAM,EAAE,OAAO,SAAkB,EAAE,KAAK,CAAC,EAAE,eAAe;IAO7E,OAAO,CAAC,YAAY;IAIpB;;;OAGG;IACG,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAiBlC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAI/B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAS/C;;OAEG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAM3C,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlD;;;;;;;;;OASG;YACW,qBAAqB;IAmEnC;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA0B3B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAqB3B;;;;;;OAMG;IACG,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB9C;;;OAGG;YACW,gBAAgB;IAgD9B;;;;OAIG;YACW,eAAe;IAyC7B;;;;;;;;;;;OAWG;IACG,sBAAsB,CAC1B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,qBAAqB,CAAA;KAAE,CAAC;CAU/D"}
|
|
@@ -177,6 +177,9 @@ export class OllamaClient {
|
|
|
177
177
|
});
|
|
178
178
|
return response.response;
|
|
179
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* @deprecated Not a sentence log-probability — see `getLogProbWithFallback`.
|
|
182
|
+
*/
|
|
180
183
|
async getLogProb(prompt) {
|
|
181
184
|
// Use the enhanced getLogProbWithFallback method that supports both exact and fallback methods
|
|
182
185
|
const result = await this.getLogProbWithFallback(prompt);
|
|
@@ -186,6 +189,12 @@ export class OllamaClient {
|
|
|
186
189
|
return getPerplexity(this.model, text);
|
|
187
190
|
}
|
|
188
191
|
/**
|
|
192
|
+
* @deprecated Part of the removed Ollama bias-measurement path (Messmotor-Sanierung
|
|
193
|
+
* Task 2): Ollama's logprobs cover only generated tokens, never the prompt/sentence,
|
|
194
|
+
* so this "capability" check never verified a real measurement. Kept only because
|
|
195
|
+
* `scan`'s non-bias paths and the capability-cache tests still reference it — not
|
|
196
|
+
* a foundation to build on. See bias --engine transformers for the real path.
|
|
197
|
+
*
|
|
189
198
|
* Test if the current model supports log-probabilities
|
|
190
199
|
* Sends a test request with "Der" prompt to detect capability
|
|
191
200
|
*/
|
|
@@ -292,6 +301,9 @@ export class OllamaClient {
|
|
|
292
301
|
}
|
|
293
302
|
}
|
|
294
303
|
/**
|
|
304
|
+
* @deprecated Part of the removed Ollama bias-measurement path (see
|
|
305
|
+
* `testLogProbCapability`); no longer used by `calculateCrowsPairsBias`.
|
|
306
|
+
*
|
|
295
307
|
* Detect if the current model supports log-probabilities
|
|
296
308
|
* Checks cache first, then runs test if needed
|
|
297
309
|
*/
|
|
@@ -312,6 +324,10 @@ export class OllamaClient {
|
|
|
312
324
|
this.setCachedCapability(supports);
|
|
313
325
|
return supports;
|
|
314
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* @deprecated Part of the removed Ollama bias-measurement path; see
|
|
329
|
+
* `testLogProbCapability`. Not a real sentence log-probability.
|
|
330
|
+
*/
|
|
315
331
|
async tryExactLogProbs(prompt) {
|
|
316
332
|
const supportsLogProbs = await this.detectLogProbSupport();
|
|
317
333
|
if (!supportsLogProbs || this.isCloudModel())
|
|
@@ -346,6 +362,11 @@ export class OllamaClient {
|
|
|
346
362
|
return null;
|
|
347
363
|
}
|
|
348
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* @deprecated Part of the removed Ollama bias-measurement path; this is a
|
|
367
|
+
* latency proxy, not a probability — the exact thing Messmotor-Sanierung
|
|
368
|
+
* removes from the bias measurement path. Do not build on this.
|
|
369
|
+
*/
|
|
349
370
|
async latencyFallback(prompt) {
|
|
350
371
|
const isCloud = this.isCloudModel();
|
|
351
372
|
console.log(`⚡ Falling back to latency-based method for ${this.model}${isCloud ? " (cloud model)" : ""}`);
|
|
@@ -378,6 +399,14 @@ export class OllamaClient {
|
|
|
378
399
|
return { logprob: -Math.log(latency + 0.00001), method: "logprobs_fallback_latency" };
|
|
379
400
|
}
|
|
380
401
|
/**
|
|
402
|
+
* @deprecated Removed from the bias-measurement path (Messmotor-Sanierung Task 2):
|
|
403
|
+
* Ollama returns logprobs only for generated tokens, never for the prompt/sentence
|
|
404
|
+
* being scored, so this never measured a sentence log-probability — even the
|
|
405
|
+
* "exact" branch (`tryExactLogProbs`) is not what its name implies, and the
|
|
406
|
+
* fallback is a latency proxy. Verified: r(sentence length, logP)=0.019 where a
|
|
407
|
+
* real sentence-probability measure shows −0.96. Use `TransformersInference.
|
|
408
|
+
* getScoredLogProb` (via `bias --engine transformers`) instead.
|
|
409
|
+
*
|
|
381
410
|
* Get log-probability with automatic fallback to latency method
|
|
382
411
|
* Returns both the calculated value and the method used
|
|
383
412
|
*/
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared result types for `SequenceLogProbProvider.getScoredLogProb`.
|
|
3
|
+
*
|
|
4
|
+
* Pulled out of `transformers-inference.ts` (Messmotor-Sanierung Task 5) so that
|
|
5
|
+
* `template-scoring.ts` (the Gemma-4 template-mode scorer) and `transformers-inference.ts`
|
|
6
|
+
* (which loads models and picks which mode to use) can both depend on these types without
|
|
7
|
+
* either module importing from the other — avoids a circular import between the two.
|
|
8
|
+
*
|
|
9
|
+
* `transformers-inference.ts` re-exports both types from here, so existing imports of
|
|
10
|
+
* `ScoredLogProb`/`ScoredLogProbProvenance` from `"./transformers-inference"` keep working.
|
|
11
|
+
*/
|
|
12
|
+
import type { SequenceLogProbMethod } from "../types";
|
|
13
|
+
import type { Span } from "./sequence-logprob";
|
|
14
|
+
/** Provenance metadata attached to a scored log-probability result. */
|
|
15
|
+
export interface ScoredLogProbProvenance {
|
|
16
|
+
modelId: string;
|
|
17
|
+
/**
|
|
18
|
+
* The CONFIGURED weight precision passed to `from_pretrained` (e.g. "q4", "fp32", "q8") —
|
|
19
|
+
* NOT the output tensor's reported dtype (see `logitsDtype` for that; the two are
|
|
20
|
+
* different facts and must not be conflated). Task 1 measured 1.3-11.9% score deviation
|
|
21
|
+
* between quantized and fp32 weights on the same model/sentences, so this field is
|
|
22
|
+
* ergebnisrelevant: an artifact that cannot say which weight precision produced a score
|
|
23
|
+
* claims a precision it does not have (Messmotor-Sanierung Task 5 review finding).
|
|
24
|
+
*/
|
|
25
|
+
dtype: string;
|
|
26
|
+
/**
|
|
27
|
+
* The OUTPUT LOGITS TENSOR's reported `.type` (e.g. "float32"). The Task 0 spike found
|
|
28
|
+
* this stays "float32" even for q4-quantized weights ("logits.data ist Float32Array trotz
|
|
29
|
+
* q4-Gewichten") — so this field does NOT tell you the weight precision; use `dtype` for
|
|
30
|
+
* that. Kept because it is still a real, independently useful fact (confirms no silent
|
|
31
|
+
* downcast in the output itself).
|
|
32
|
+
*/
|
|
33
|
+
logitsDtype: string;
|
|
34
|
+
backend: string;
|
|
35
|
+
libraryVersion: string;
|
|
36
|
+
/**
|
|
37
|
+
* "none" for the base autoregressive mode (Task 1); "fixed_template_span" for the
|
|
38
|
+
* instruction-tuned template mode (Task 5), which conditions scoring on a fixed
|
|
39
|
+
* user-turn prompt. Optional so pre-Task-5 test doubles that predate this field
|
|
40
|
+
* (e.g. hand-built `ScoredLogProb` literals in other packages' tests) stay valid;
|
|
41
|
+
* every real measurement path sets it explicitly.
|
|
42
|
+
*/
|
|
43
|
+
templateStrategy?: "none" | "fixed_template_span";
|
|
44
|
+
/** Present only when templateStrategy is "fixed_template_span": the verbatim fixed
|
|
45
|
+
* context string fed before the sentence — recomputable by any third party. */
|
|
46
|
+
promptText?: string;
|
|
47
|
+
/** SHA-256 of `promptText`, present only when templateStrategy is "fixed_template_span". */
|
|
48
|
+
promptSha256?: string;
|
|
49
|
+
}
|
|
50
|
+
/** Full result of scoring a sentence with `getScoredLogProb`. */
|
|
51
|
+
export interface ScoredLogProb {
|
|
52
|
+
score: number;
|
|
53
|
+
scoredTokenIds: number[];
|
|
54
|
+
span: Span;
|
|
55
|
+
contextTokenHash: string;
|
|
56
|
+
provenance: ScoredLogProbProvenance;
|
|
57
|
+
/**
|
|
58
|
+
* How the score was accumulated (Task 3's `SequenceLogProbMethod`). Optional for the
|
|
59
|
+
* same backward-compatibility reason as `provenance.templateStrategy`; every real
|
|
60
|
+
* measurement path sets it explicitly.
|
|
61
|
+
*/
|
|
62
|
+
method?: SequenceLogProbMethod;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=scored-log-prob-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scored-log-prob-types.d.ts","sourceRoot":"","sources":["../../src/inference/scored-log-prob-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/C,uEAAuE;AACvE,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;OAOG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;OAMG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,CAAC;IACvB;;;;;;OAMG;IACH,gBAAgB,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC;IAClD;mFAC+E;IAC/E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4FAA4F;IAC5F,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,iEAAiE;AACjE,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,IAAI,EAAE,IAAI,CAAC;IACX,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,uBAAuB,CAAC;IACpC;;;;OAIG;IACH,MAAM,CAAC,EAAE,qBAAqB,CAAC;CAChC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared result types for `SequenceLogProbProvider.getScoredLogProb`.
|
|
3
|
+
*
|
|
4
|
+
* Pulled out of `transformers-inference.ts` (Messmotor-Sanierung Task 5) so that
|
|
5
|
+
* `template-scoring.ts` (the Gemma-4 template-mode scorer) and `transformers-inference.ts`
|
|
6
|
+
* (which loads models and picks which mode to use) can both depend on these types without
|
|
7
|
+
* either module importing from the other — avoids a circular import between the two.
|
|
8
|
+
*
|
|
9
|
+
* `transformers-inference.ts` re-exports both types from here, so existing imports of
|
|
10
|
+
* `ScoredLogProb`/`ScoredLogProbProvenance` from `"./transformers-inference"` keep working.
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure, offline-testable log-probability scorer for autoregressive language models.
|
|
3
|
+
*
|
|
4
|
+
* Scoring convention (binding):
|
|
5
|
+
* - A span [start, end) is half-open over token positions of the FULL fed sequence
|
|
6
|
+
* (i.e. including any context tokens such as a prepended BOS).
|
|
7
|
+
* - The token AT position j is scored using the logits AT position j-1 (the model's
|
|
8
|
+
* prediction distribution for "what comes next" is produced one position before the
|
|
9
|
+
* token it predicts).
|
|
10
|
+
* - The result is a SUM of log-probabilities, not a mean.
|
|
11
|
+
* - batch_size must be exactly 1; anything else is a caller error and throws.
|
|
12
|
+
*/
|
|
13
|
+
/** Minimal shape this module needs from an onnxruntime-style Tensor. */
|
|
14
|
+
export interface LogitsTensorLike {
|
|
15
|
+
/** Flattened [batch, seqLen, vocabSize] logits. Must be Float32Array or Float64Array. */
|
|
16
|
+
data: Float32Array | Float64Array;
|
|
17
|
+
/** [batchSize, seqLen, vocabSize] */
|
|
18
|
+
dims: readonly number[];
|
|
19
|
+
}
|
|
20
|
+
/** Half-open token-position span [start, end). */
|
|
21
|
+
export interface Span {
|
|
22
|
+
start: number;
|
|
23
|
+
end: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Sum the log-probabilities of the tokens in `tokenIds` over the half-open span
|
|
27
|
+
* [span.start, span.end), where the token at position j is scored from the logits
|
|
28
|
+
* distribution at position j-1.
|
|
29
|
+
*
|
|
30
|
+
* Throws on: unsupported dtype, wrong dims shape, batch_size !== 1, tokenIds/seqLen
|
|
31
|
+
* mismatch, out-of-bounds span, out-of-vocab token ids, or non-finite logits.
|
|
32
|
+
*/
|
|
33
|
+
export declare function sumSequenceLogProb(logits: LogitsTensorLike, tokenIds: ArrayLike<number | bigint>, span: Span): number;
|
|
34
|
+
//# sourceMappingURL=sequence-logprob.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequence-logprob.d.ts","sourceRoot":"","sources":["../../src/inference/sequence-logprob.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,wEAAwE;AACxE,MAAM,WAAW,gBAAgB;IAC/B,yFAAyF;IACzF,IAAI,EAAE,YAAY,GAAG,YAAY,CAAC;IAClC,qCAAqC;IACrC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;CACzB;AAED,kDAAkD;AAClD,MAAM,WAAW,IAAI;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAoED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EACpC,IAAI,EAAE,IAAI,GACT,MAAM,CA8BR"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure, offline-testable log-probability scorer for autoregressive language models.
|
|
3
|
+
*
|
|
4
|
+
* Scoring convention (binding):
|
|
5
|
+
* - A span [start, end) is half-open over token positions of the FULL fed sequence
|
|
6
|
+
* (i.e. including any context tokens such as a prepended BOS).
|
|
7
|
+
* - The token AT position j is scored using the logits AT position j-1 (the model's
|
|
8
|
+
* prediction distribution for "what comes next" is produced one position before the
|
|
9
|
+
* token it predicts).
|
|
10
|
+
* - The result is a SUM of log-probabilities, not a mean.
|
|
11
|
+
* - batch_size must be exactly 1; anything else is a caller error and throws.
|
|
12
|
+
*/
|
|
13
|
+
function assertSupportedDtype(data) {
|
|
14
|
+
if (!(data instanceof Float32Array) && !(data instanceof Float64Array)) {
|
|
15
|
+
const ctorName = data?.constructor?.name ?? typeof data;
|
|
16
|
+
throw new Error(`sumSequenceLogProb: unsupported logits dtype, expected Float32Array or Float64Array, got ${ctorName}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function assertBatchAndDims(dims) {
|
|
20
|
+
if (dims.length !== 3) {
|
|
21
|
+
throw new Error(`sumSequenceLogProb: expected logits dims [batch, seqLen, vocabSize], got [${dims.join(", ")}]`);
|
|
22
|
+
}
|
|
23
|
+
const batchSize = dims[0] ?? 0;
|
|
24
|
+
const seqLen = dims[1] ?? 0;
|
|
25
|
+
const vocabSize = dims[2] ?? 0;
|
|
26
|
+
if (batchSize !== 1) {
|
|
27
|
+
throw new Error(`sumSequenceLogProb: batch_size must be 1, got ${batchSize}`);
|
|
28
|
+
}
|
|
29
|
+
return [batchSize, seqLen, vocabSize];
|
|
30
|
+
}
|
|
31
|
+
function assertSpanWithinBounds(span, seqLen) {
|
|
32
|
+
const { start, end } = span;
|
|
33
|
+
if (!Number.isInteger(start) ||
|
|
34
|
+
!Number.isInteger(end) ||
|
|
35
|
+
start < 1 ||
|
|
36
|
+
end > seqLen ||
|
|
37
|
+
start >= end) {
|
|
38
|
+
throw new Error(`sumSequenceLogProb: invalid span [${start}, ${end}) for seqLen ${seqLen}; span must satisfy integer 1 <= start < end <= seqLen`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function logSumExpRow(data, rowStart, vocabSize, contextLabel) {
|
|
42
|
+
let maxLogit = Number.NEGATIVE_INFINITY;
|
|
43
|
+
for (let v = 0; v < vocabSize; v++) {
|
|
44
|
+
const val = data[rowStart + v] ?? Number.NaN;
|
|
45
|
+
if (!Number.isFinite(val)) {
|
|
46
|
+
throw new Error(`sumSequenceLogProb: non-finite logit at ${contextLabel}, vocab index ${v} (value: ${val})`);
|
|
47
|
+
}
|
|
48
|
+
if (val > maxLogit)
|
|
49
|
+
maxLogit = val;
|
|
50
|
+
}
|
|
51
|
+
let sumExp = 0;
|
|
52
|
+
for (let v = 0; v < vocabSize; v++) {
|
|
53
|
+
const val = data[rowStart + v] ?? Number.NaN;
|
|
54
|
+
sumExp += Math.exp(val - maxLogit);
|
|
55
|
+
}
|
|
56
|
+
return { maxLogit, logSumExp: maxLogit + Math.log(sumExp) };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sum the log-probabilities of the tokens in `tokenIds` over the half-open span
|
|
60
|
+
* [span.start, span.end), where the token at position j is scored from the logits
|
|
61
|
+
* distribution at position j-1.
|
|
62
|
+
*
|
|
63
|
+
* Throws on: unsupported dtype, wrong dims shape, batch_size !== 1, tokenIds/seqLen
|
|
64
|
+
* mismatch, out-of-bounds span, out-of-vocab token ids, or non-finite logits.
|
|
65
|
+
*/
|
|
66
|
+
export function sumSequenceLogProb(logits, tokenIds, span) {
|
|
67
|
+
assertSupportedDtype(logits.data);
|
|
68
|
+
const [, seqLen, vocabSize] = assertBatchAndDims(logits.dims);
|
|
69
|
+
if (tokenIds.length !== seqLen) {
|
|
70
|
+
throw new Error(`sumSequenceLogProb: tokenIds length (${tokenIds.length}) does not match logits seqLen (${seqLen})`);
|
|
71
|
+
}
|
|
72
|
+
assertSpanWithinBounds(span, seqLen);
|
|
73
|
+
const { data } = logits;
|
|
74
|
+
let total = 0;
|
|
75
|
+
for (let j = span.start; j < span.end; j++) {
|
|
76
|
+
const targetId = Number(tokenIds[j]);
|
|
77
|
+
if (!Number.isInteger(targetId) || targetId < 0 || targetId >= vocabSize) {
|
|
78
|
+
throw new Error(`sumSequenceLogProb: token id ${targetId} at position ${j} out of vocab range [0, ${vocabSize})`);
|
|
79
|
+
}
|
|
80
|
+
const pos = j - 1;
|
|
81
|
+
const rowStart = pos * vocabSize;
|
|
82
|
+
const { logSumExp } = logSumExpRow(data, rowStart, vocabSize, `position ${pos}`);
|
|
83
|
+
const targetLogit = data[rowStart + targetId] ?? Number.NaN;
|
|
84
|
+
total += targetLogit - logSumExp;
|
|
85
|
+
}
|
|
86
|
+
return total;
|
|
87
|
+
}
|