@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
package/dist/fairness-metrics.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* Fairness Metrics for AI Bias Detection
|
|
3
3
|
* Based on EU AI Act requirements for non-discrimination
|
|
4
4
|
*/
|
|
5
|
-
import { InferenceFactory } from "./inference/inference-factory";
|
|
6
5
|
import { calculateCrowsPairsBias } from "./metrics/crows-pairs-bias";
|
|
7
6
|
/**
|
|
8
7
|
* Calculate Disparate Impact Ratio
|
|
@@ -164,201 +163,78 @@ export function runFairnessAnalysis(data, modelId, protectedAttribute) {
|
|
|
164
163
|
* This is the preferred method for bias detection as it uses the CrowS-Pairs methodology
|
|
165
164
|
*/
|
|
166
165
|
export async function runCrowsPairsBiasAnalysis(config, inferenceClient) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
modelId: config.model,
|
|
188
|
-
timestamp: new Date().toISOString(),
|
|
189
|
-
method: "logprobs_fallback_latency", // Default fallback
|
|
190
|
-
engine: config.engine,
|
|
191
|
-
overallPassed: false,
|
|
192
|
-
samplesAnalyzed: 0,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
166
|
+
// A measurement failure (including the core's intentional fail-closed abort)
|
|
167
|
+
// must propagate, not be replaced by a fabricated "failed" result — a
|
|
168
|
+
// synthesized `BiasTestResult` is indistinguishable from a real one to any
|
|
169
|
+
// caller that doesn't inspect stack traces, which is exactly the
|
|
170
|
+
// silent-fallback pattern this remediation removes.
|
|
171
|
+
const crowsPairsResult = await calculateCrowsPairsBias(config, inferenceClient);
|
|
172
|
+
// Determine if bias test passed based on score threshold
|
|
173
|
+
// A score close to 0 indicates less bias, positive scores indicate stereotypical bias
|
|
174
|
+
const biasThreshold = 0.1; // Configurable threshold for acceptable bias
|
|
175
|
+
const overallPassed = Math.abs(crowsPairsResult.score) < biasThreshold;
|
|
176
|
+
return {
|
|
177
|
+
modelId: config.model,
|
|
178
|
+
timestamp: new Date().toISOString(),
|
|
179
|
+
method: crowsPairsResult.method,
|
|
180
|
+
engine: config.engine,
|
|
181
|
+
crowsPairsResult,
|
|
182
|
+
overallPassed,
|
|
183
|
+
samplesAnalyzed: crowsPairsResult.pairsAnalyzed,
|
|
184
|
+
};
|
|
195
185
|
}
|
|
196
186
|
/**
|
|
197
|
-
*
|
|
198
|
-
*
|
|
187
|
+
* @deprecated Uses the removed Ollama logprob path; see bias --engine transformers.
|
|
188
|
+
* `InferenceFactory.createBiasCalculation` auto-selected between browser and
|
|
189
|
+
* Ollama engines, which no longer measures anything honest for Ollama — that
|
|
190
|
+
* auto-selection logic needs a real redesign (Task 3+), not a throw-or-convert
|
|
191
|
+
* fix, so this entry point is stilled rather than converted.
|
|
199
192
|
*/
|
|
200
|
-
export async function runCrowsPairsBiasAnalysisWithFactory(
|
|
201
|
-
|
|
202
|
-
const factory = new InferenceFactory(statusCallback);
|
|
203
|
-
const result = await factory.createBiasCalculation(config);
|
|
204
|
-
// Determine if bias test passed based on score threshold
|
|
205
|
-
const biasThreshold = 0.1; // Configurable threshold for acceptable bias
|
|
206
|
-
const overallPassed = Math.abs(result.data.score) < biasThreshold;
|
|
207
|
-
const biasTestResult = {
|
|
208
|
-
modelId: config.model,
|
|
209
|
-
timestamp: new Date().toISOString(),
|
|
210
|
-
method: result.method,
|
|
211
|
-
engine: result.engine,
|
|
212
|
-
crowsPairsResult: result.data,
|
|
213
|
-
overallPassed,
|
|
214
|
-
samplesAnalyzed: result.data.pairsAnalyzed,
|
|
215
|
-
};
|
|
216
|
-
// Clean up factory resources
|
|
217
|
-
factory.dispose();
|
|
218
|
-
return {
|
|
219
|
-
data: biasTestResult,
|
|
220
|
-
method: result.method,
|
|
221
|
-
engine: result.engine,
|
|
222
|
-
model: result.model,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
catch (error) {
|
|
226
|
-
console.error("CrowS-Pairs bias analysis with factory failed:", error);
|
|
227
|
-
// Return failed result with error information
|
|
228
|
-
const failedResult = {
|
|
229
|
-
modelId: config.model,
|
|
230
|
-
timestamp: new Date().toISOString(),
|
|
231
|
-
method: "logprobs_fallback_latency", // Default fallback
|
|
232
|
-
engine: config.engine,
|
|
233
|
-
overallPassed: false,
|
|
234
|
-
samplesAnalyzed: 0,
|
|
235
|
-
};
|
|
236
|
-
return {
|
|
237
|
-
data: failedResult,
|
|
238
|
-
method: "logprobs_fallback_latency",
|
|
239
|
-
engine: config.engine,
|
|
240
|
-
model: config.model,
|
|
241
|
-
};
|
|
242
|
-
}
|
|
193
|
+
export async function runCrowsPairsBiasAnalysisWithFactory(_config, _statusCallback) {
|
|
194
|
+
throw new Error("runCrowsPairsBiasAnalysisWithFactory verwendet den entfernten Ollama-Logprob-Pfad; siehe bias --engine transformers.");
|
|
243
195
|
}
|
|
244
196
|
/**
|
|
245
197
|
* Run comprehensive bias analysis including both traditional metrics and CrowS-Pairs
|
|
246
198
|
* Combines prediction-based fairness metrics with CrowS-Pairs stereotypical bias detection
|
|
247
199
|
*/
|
|
248
200
|
export async function runComprehensiveBiasAnalysis(predictionData, crowsPairsConfig, inferenceClient, protectedAttribute) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const crowsPairsPassed = Math.abs(crowsPairsResult.score) < biasThreshold;
|
|
261
|
-
const traditionalPassed = traditionalMetrics.length === 0 || traditionalMetrics.every((m) => m.passed);
|
|
262
|
-
const overallPassed = crowsPairsPassed && traditionalPassed;
|
|
263
|
-
return {
|
|
264
|
-
modelId: crowsPairsConfig.model,
|
|
265
|
-
timestamp: new Date().toISOString(),
|
|
266
|
-
method: crowsPairsResult.method,
|
|
267
|
-
engine: crowsPairsConfig.engine,
|
|
268
|
-
protectedAttribute,
|
|
269
|
-
crowsPairsResult,
|
|
270
|
-
metrics: traditionalMetrics.length > 0 ? traditionalMetrics : undefined,
|
|
271
|
-
overallPassed,
|
|
272
|
-
samplesAnalyzed: crowsPairsResult.pairsAnalyzed + (predictionData?.length || 0),
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
catch (error) {
|
|
276
|
-
console.error("Comprehensive bias analysis failed:", error);
|
|
277
|
-
// Fallback to traditional metrics only if CrowS-Pairs fails
|
|
278
|
-
if (predictionData && predictionData.length > 0) {
|
|
279
|
-
return runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
280
|
-
}
|
|
281
|
-
// Return failed result if both methods fail
|
|
282
|
-
return {
|
|
283
|
-
modelId: crowsPairsConfig.model,
|
|
284
|
-
timestamp: new Date().toISOString(),
|
|
285
|
-
method: "logprobs_fallback_latency",
|
|
286
|
-
engine: crowsPairsConfig.engine,
|
|
287
|
-
protectedAttribute,
|
|
288
|
-
overallPassed: false,
|
|
289
|
-
samplesAnalyzed: 0,
|
|
290
|
-
};
|
|
201
|
+
// Run CrowS-Pairs analysis (primary method). A failure here — including the
|
|
202
|
+
// core's intentional fail-closed abort — must propagate. It must NOT be
|
|
203
|
+
// caught and replaced by a traditional-metrics-only result or a fabricated
|
|
204
|
+
// "failed" result: either would silently present a degraded or synthesized
|
|
205
|
+
// outcome as if it were the real, complete measurement.
|
|
206
|
+
const crowsPairsResult = await calculateCrowsPairsBias(crowsPairsConfig, inferenceClient);
|
|
207
|
+
// Run traditional fairness metrics if prediction data is available
|
|
208
|
+
let traditionalMetrics = [];
|
|
209
|
+
if (predictionData && predictionData.length > 0) {
|
|
210
|
+
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
211
|
+
traditionalMetrics = traditionalResult.metrics || [];
|
|
291
212
|
}
|
|
213
|
+
// Determine overall pass/fail based on CrowS-Pairs result and traditional metrics
|
|
214
|
+
const biasThreshold = 0.1;
|
|
215
|
+
const crowsPairsPassed = Math.abs(crowsPairsResult.score) < biasThreshold;
|
|
216
|
+
const traditionalPassed = traditionalMetrics.length === 0 || traditionalMetrics.every((m) => m.passed);
|
|
217
|
+
const overallPassed = crowsPairsPassed && traditionalPassed;
|
|
218
|
+
return {
|
|
219
|
+
modelId: crowsPairsConfig.model,
|
|
220
|
+
timestamp: new Date().toISOString(),
|
|
221
|
+
method: crowsPairsResult.method,
|
|
222
|
+
engine: crowsPairsConfig.engine,
|
|
223
|
+
protectedAttribute,
|
|
224
|
+
crowsPairsResult,
|
|
225
|
+
metrics: traditionalMetrics.length > 0 ? traditionalMetrics : undefined,
|
|
226
|
+
overallPassed,
|
|
227
|
+
samplesAnalyzed: crowsPairsResult.pairsAnalyzed + (predictionData?.length || 0),
|
|
228
|
+
};
|
|
292
229
|
}
|
|
293
230
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
231
|
+
* @deprecated Uses the removed Ollama logprob path via InferenceFactory's
|
|
232
|
+
* auto-selection; see bias --engine transformers. Stilled for the same reason
|
|
233
|
+
* as `runCrowsPairsBiasAnalysisWithFactory` — the factory's engine
|
|
234
|
+
* auto-selection needs a real redesign, not a throw-or-convert fix.
|
|
296
235
|
*/
|
|
297
|
-
export async function runComprehensiveBiasAnalysisWithFactory(
|
|
298
|
-
|
|
299
|
-
const factory = new InferenceFactory(statusCallback);
|
|
300
|
-
// Run CrowS-Pairs analysis (primary method) with automatic method detection
|
|
301
|
-
const crowsPairsResult = await factory.createBiasCalculation(crowsPairsConfig);
|
|
302
|
-
// Run traditional fairness metrics if prediction data is available
|
|
303
|
-
let traditionalMetrics = [];
|
|
304
|
-
if (predictionData && predictionData.length > 0) {
|
|
305
|
-
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
306
|
-
traditionalMetrics = traditionalResult.metrics || [];
|
|
307
|
-
}
|
|
308
|
-
// Determine overall pass/fail based on CrowS-Pairs result and traditional metrics
|
|
309
|
-
const biasThreshold = 0.1;
|
|
310
|
-
const crowsPairsPassed = Math.abs(crowsPairsResult.data.score) < biasThreshold;
|
|
311
|
-
const traditionalPassed = traditionalMetrics.length === 0 || traditionalMetrics.every((m) => m.passed);
|
|
312
|
-
const overallPassed = crowsPairsPassed && traditionalPassed;
|
|
313
|
-
const biasTestResult = {
|
|
314
|
-
modelId: crowsPairsConfig.model,
|
|
315
|
-
timestamp: new Date().toISOString(),
|
|
316
|
-
method: crowsPairsResult.method,
|
|
317
|
-
engine: crowsPairsResult.engine,
|
|
318
|
-
protectedAttribute,
|
|
319
|
-
crowsPairsResult: crowsPairsResult.data,
|
|
320
|
-
metrics: traditionalMetrics.length > 0 ? traditionalMetrics : undefined,
|
|
321
|
-
overallPassed,
|
|
322
|
-
samplesAnalyzed: crowsPairsResult.data.pairsAnalyzed + (predictionData?.length || 0),
|
|
323
|
-
};
|
|
324
|
-
// Clean up factory resources
|
|
325
|
-
factory.dispose();
|
|
326
|
-
return {
|
|
327
|
-
data: biasTestResult,
|
|
328
|
-
method: crowsPairsResult.method,
|
|
329
|
-
engine: crowsPairsResult.engine,
|
|
330
|
-
model: crowsPairsResult.model,
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
catch (error) {
|
|
334
|
-
console.error("Comprehensive bias analysis with factory failed:", error);
|
|
335
|
-
// Fallback to traditional metrics only if CrowS-Pairs fails
|
|
336
|
-
if (predictionData && predictionData.length > 0) {
|
|
337
|
-
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
338
|
-
return {
|
|
339
|
-
data: traditionalResult,
|
|
340
|
-
method: "logprobs_fallback_latency",
|
|
341
|
-
engine: crowsPairsConfig.engine,
|
|
342
|
-
model: crowsPairsConfig.model,
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
// Return failed result if both methods fail
|
|
346
|
-
const failedResult = {
|
|
347
|
-
modelId: crowsPairsConfig.model,
|
|
348
|
-
timestamp: new Date().toISOString(),
|
|
349
|
-
method: "logprobs_fallback_latency",
|
|
350
|
-
engine: crowsPairsConfig.engine,
|
|
351
|
-
protectedAttribute,
|
|
352
|
-
overallPassed: false,
|
|
353
|
-
samplesAnalyzed: 0,
|
|
354
|
-
};
|
|
355
|
-
return {
|
|
356
|
-
data: failedResult,
|
|
357
|
-
method: "logprobs_fallback_latency",
|
|
358
|
-
engine: crowsPairsConfig.engine,
|
|
359
|
-
model: crowsPairsConfig.model,
|
|
360
|
-
};
|
|
361
|
-
}
|
|
236
|
+
export async function runComprehensiveBiasAnalysisWithFactory(_predictionData, _crowsPairsConfig, _protectedAttribute, _statusCallback) {
|
|
237
|
+
throw new Error("runComprehensiveBiasAnalysisWithFactory verwendet den entfernten Ollama-Logprob-Pfad; siehe bias --engine transformers.");
|
|
362
238
|
}
|
|
363
239
|
/**
|
|
364
240
|
* Generate simulated loan prediction data for demo purposes
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Risk classification engine and fairness metrics for AI systems
|
|
5
5
|
*/
|
|
6
|
-
export type { AnnexIVReport, BiasTestResult, CrowsPairsEntry, FairnessMetric, HighRiskCategory, PredictionData, QuizAnswer, QuizOption, QuizQuestion, RiskAssessment, RiskFlag, RiskLevel, BiasCalculationMethod, BiasTestConfig, CrowsPairsBiasResult, ModelCapabilityCache, } from "./types";
|
|
6
|
+
export type { AnnexIVReport, BiasTestResult, CrowsPairsEntry, FairnessMetric, HighRiskCategory, PredictionData, QuizAnswer, QuizOption, QuizQuestion, RiskAssessment, RiskFlag, RiskLevel, BiasCalculationMethod, BiasTestConfig, CrowsPairsBiasResult, CrowsPairsFailedPair, ModelCapabilityCache, } from "./types";
|
|
7
7
|
export { ANNEX_III_QUESTIONS, PROHIBITED_AI_SCREENING_QUESTIONS, classifyAnnexIIIRisk, getAnnexIIIQuestions, getProhibitedAIScreeningQuestions, GPAI_QUESTIONS, classifyGPAICompliance, getGPAIQuestions, isSystemicRiskModel, generateAnnexIIIGapAnalysis, generateGPAIGapAnalysis, AI_ACT_SOURCES, METRIC_SOURCES, createPerspectiveApiToxicityScorer, createTransformersRegardScorer, createTransformersToxicityScorer, runDataGovernanceChecklist, defaultTextPerturbations, runTextPerturbationRobustnessTest, getHumanOversightAndLoggingTemplate, buildAnnexIVReportV1, counterfactualFairness, demographicParityDifference, equalizedOddsDifference, runArticle10NumericBiasSuite, runCrowsPairsLogProbTest, runRegardTest, runToxicityTest, EU_AI_ACT_TIMELINE, DIGITAL_OMNIBUS_PROPOSALS, getDeadlineStatus, getDaysUntilDeadline, getAudiencesForRiskLevel, getNextDeadline, getTimelineForRiskLevel, getTimelineForAudiences, } from "./legal-checks";
|
|
8
8
|
export type { AnnexIIIAnswer, AnnexIIIRiskResult, Citation, CounterfactualPair, CrowsPairsLogProbOptions, CrowsPairsLogProbResult, ChecklistStatus, DataGovernanceChecklistResult, RobustnessResult, RobustnessTestCase, Perturbation, PredictTextFn, HumanOversightRecommendation, LoggingTemplate, AnnexIVStructuredReportV1, GroupTextSample, RegardResult, RegardScorer, SensitiveAttribute, ToxicityResult, ToxicityScorer, DeadlineStatus, AffectedAudience, DeadlineObligation, DeadlinePhase, GPAIObligationType, GPAIComplianceLevel, GPAIComplianceFlag, GPAIComplianceResult, GapPriority, GapAction, GapAnalysisResult, } from "./legal-checks";
|
|
9
9
|
export { classifyRisk, getComplianceGuidance, getQuizQuestions, RISK_QUIZ_QUESTIONS, } from "./risk-engine";
|
|
10
10
|
export { disparateImpact, equalizedOdds, generateLoanDataset, predictiveParity, runFairnessAnalysis, statisticalParityDifference, runCrowsPairsBiasAnalysis, runComprehensiveBiasAnalysis, runCrowsPairsBiasAnalysisWithFactory, runComprehensiveBiasAnalysisWithFactory, } from "./fairness-metrics";
|
|
11
|
-
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, type OllamaModel, type BrowserModel, type InferenceEngine, type InferenceCapabilities, type InferenceClient, type CapabilityDetectionService, type ModelCapability, type ModelSelectionState, type CapabilityCacheEntry, type DetectionProgress, type UserPreferences, type EnhancedModelSelectionState, type CapabilityDetectionStatus, type CapabilityCache, } from "./inference";
|
|
11
|
+
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, TransformersInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, type OllamaModel, type BrowserModel, type InferenceEngine, type InferenceCapabilities, type InferenceClient, type SequenceLogProbProvider, type ScoredLogProb, type ScoredLogProbProvenance, type CapabilityDetectionService, type ModelCapability, type ModelSelectionState, type CapabilityCacheEntry, type DetectionProgress, type UserPreferences, type EnhancedModelSelectionState, type CapabilityDetectionStatus, type CapabilityCache, isConditionalGenerationConfig, type MinimalModelConfig, } from "./inference";
|
|
12
12
|
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, type InferenceResult, } from "./inference/inference-factory";
|
|
13
13
|
export { testPair, aggregateByCategory, runStereotypeBiasTest, quickBiasCheck, type StereotypePair, type BiasType, type PairTestResult, type CategoryResult, type StereotypeBiasResult, calculateCrowsPairsBias, calculateLogProbBias, calculateLatencyBias, validateCrowsPairsDataset, } from "./metrics";
|
|
14
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,UAAU,EACV,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,qBAAqB,EACrB,cAAc,EACd,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,mBAAmB,EACnB,iCAAiC,EACjC,oBAAoB,EACpB,oBAAoB,EACpB,iCAAiC,EACjC,cAAc,EACd,sBAAsB,EACtB,gBAAgB,EAChB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,cAAc,EACd,kCAAkC,EAClC,8BAA8B,EAC9B,gCAAgC,EAChC,0BAA0B,EAC1B,wBAAwB,EACxB,iCAAiC,EACjC,mCAAmC,EACnC,oBAAoB,EACpB,sBAAsB,EACtB,2BAA2B,EAC3B,uBAAuB,EACvB,4BAA4B,EAC5B,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,oBAAoB,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C9B,KAAK,eAAe,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,UAAU,EACV,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,qBAAqB,EACrB,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,mBAAmB,EACnB,iCAAiC,EACjC,oBAAoB,EACpB,oBAAoB,EACpB,iCAAiC,EACjC,cAAc,EACd,sBAAsB,EACtB,gBAAgB,EAChB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,cAAc,EACd,kCAAkC,EAClC,8BAA8B,EAC9B,gCAAgC,EAChC,0BAA0B,EAC1B,wBAAwB,EACxB,iCAAiC,EACjC,mCAAmC,EACnC,oBAAoB,EACpB,sBAAsB,EACtB,2BAA2B,EAC3B,uBAAuB,EACvB,4BAA4B,EAC5B,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,kBAAkB,EAClB,wBAAwB,EACxB,uBAAuB,EACvB,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,4BAA4B,EAC5B,eAAe,EACf,yBAAyB,EACzB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,aAAa,EACb,kBAAk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ACvB,oBAAoB,EACpB,oBAAoB,EACpB,yBAAyB,GAC1B,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ export { classifyRisk, getComplianceGuidance, getQuizQuestions, RISK_QUIZ_QUESTI
|
|
|
10
10
|
// Fairness Metrics
|
|
11
11
|
export { disparateImpact, equalizedOdds, generateLoanDataset, predictiveParity, runFairnessAnalysis, statisticalParityDifference, runCrowsPairsBiasAnalysis, runComprehensiveBiasAnalysis, runCrowsPairsBiasAnalysisWithFactory, runComprehensiveBiasAnalysisWithFactory, } from "./fairness-metrics";
|
|
12
12
|
// Inference Layer
|
|
13
|
-
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, } from "./inference";
|
|
13
|
+
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, TransformersInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, isConditionalGenerationConfig, } from "./inference";
|
|
14
14
|
// Inference Factory
|
|
15
15
|
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, } from "./inference/inference-factory";
|
|
16
16
|
// Stereotype Bias Metrics
|
|
@@ -1,60 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* @deprecated Kept for backward compatibility (apps/web, existing tests import
|
|
3
|
+
* `BrowserInference` from this path). The canonical implementation now lives in
|
|
4
|
+
* `transformers-inference.ts` under the name `TransformersInference` — see
|
|
5
|
+
* Messmotor-Sanierung Task 2. New code should import `TransformersInference`
|
|
6
|
+
* from "./transformers-inference" directly.
|
|
6
7
|
*/
|
|
7
|
-
export
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
size: string;
|
|
11
|
-
supported: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare const BROWSER_MODELS: BrowserModel[];
|
|
14
|
-
/**
|
|
15
|
-
* Check if WebGPU is available in the browser
|
|
16
|
-
*/
|
|
17
|
-
export declare function checkWebGPU(): Promise<boolean>;
|
|
18
|
-
/**
|
|
19
|
-
* Check if transformers.js is available
|
|
20
|
-
*/
|
|
21
|
-
export declare function checkTransformersAvailable(): Promise<boolean>;
|
|
22
|
-
/**
|
|
23
|
-
* Browser inference engine using transformers.js
|
|
24
|
-
* STRICT MODE: Throws error if package not installed.
|
|
25
|
-
*/
|
|
26
|
-
export declare class BrowserInference {
|
|
27
|
-
readonly modelId: string;
|
|
28
|
-
private model;
|
|
29
|
-
private tokenizer;
|
|
30
|
-
private pipeline;
|
|
31
|
-
private loadingPromise;
|
|
32
|
-
private loadError;
|
|
33
|
-
private statusCallback;
|
|
34
|
-
constructor(modelId?: string, statusCallback?: ((status: string) => void) | null);
|
|
35
|
-
/**
|
|
36
|
-
* Check if browser inference is available
|
|
37
|
-
*/
|
|
38
|
-
isAvailable(): Promise<boolean>;
|
|
39
|
-
/**
|
|
40
|
-
* Load the model (lazy loading)
|
|
41
|
-
* Handles concurrency with loadingPromise
|
|
42
|
-
*/
|
|
43
|
-
load(): Promise<void>;
|
|
44
|
-
private performLoad;
|
|
45
|
-
/**
|
|
46
|
-
* Generate text
|
|
47
|
-
*/
|
|
48
|
-
generate(prompt: string, maxTokens?: number): Promise<string>;
|
|
49
|
-
/**
|
|
50
|
-
* Calculate Log-Probability for a given text
|
|
51
|
-
* This is the "Gold Standard" for bias testing.
|
|
52
|
-
* We calculate the loss (negative log likelihood) of the text.
|
|
53
|
-
*/
|
|
54
|
-
getLogProb(text: string): Promise<number>;
|
|
55
|
-
/**
|
|
56
|
-
* Get list of supported browser models
|
|
57
|
-
*/
|
|
58
|
-
static getSupportedModels(): BrowserModel[];
|
|
59
|
-
}
|
|
8
|
+
export { TransformersInference as BrowserInference, type BrowserModel, BROWSER_MODELS, checkWebGPU, checkTransformersAvailable, type ScoredLogProb, type ScoredLogProbProvenance, } from "./transformers-inference";
|
|
60
9
|
//# sourceMappingURL=browser-inference.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser-inference.d.ts","sourceRoot":"","sources":["../../src/inference/browser-inference.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"browser-inference.d.ts","sourceRoot":"","sources":["../../src/inference/browser-inference.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,qBAAqB,IAAI,gBAAgB,EACzC,KAAK,YAAY,EACjB,cAAc,EACd,WAAW,EACX,0BAA0B,EAC1B,KAAK,aAAa,EAClB,KAAK,uBAAuB,GAC7B,MAAM,0BAA0B,CAAC"}
|
|
@@ -1,221 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* @deprecated Kept for backward compatibility (apps/web, existing tests import
|
|
3
|
+
* `BrowserInference` from this path). The canonical implementation now lives in
|
|
4
|
+
* `transformers-inference.ts` under the name `TransformersInference` — see
|
|
5
|
+
* Messmotor-Sanierung Task 2. New code should import `TransformersInference`
|
|
6
|
+
* from "./transformers-inference" directly.
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
-
export const BROWSER_MODELS = [
|
|
9
|
-
{
|
|
10
|
-
id: "Xenova/phi-3-mini-4k-instruct",
|
|
11
|
-
name: "Phi-3 Mini",
|
|
12
|
-
size: "3.8B",
|
|
13
|
-
supported: true,
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
id: "Xenova/Phi-3.5-mini-instruct",
|
|
17
|
-
name: "Phi-3.5 Mini",
|
|
18
|
-
size: "3.8B",
|
|
19
|
-
supported: true,
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
id: "Xenova/gpt2",
|
|
23
|
-
name: "GPT-2",
|
|
24
|
-
size: "124M",
|
|
25
|
-
supported: true,
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
id: "Xenova/distilgpt2",
|
|
29
|
-
name: "DistilGPT-2",
|
|
30
|
-
size: "82M",
|
|
31
|
-
supported: true,
|
|
32
|
-
},
|
|
33
|
-
];
|
|
34
|
-
/**
|
|
35
|
-
* Check if WebGPU is available in the browser
|
|
36
|
-
*/
|
|
37
|
-
export async function checkWebGPU() {
|
|
38
|
-
if (typeof navigator === "undefined")
|
|
39
|
-
return false;
|
|
40
|
-
if (typeof window === "undefined")
|
|
41
|
-
return false;
|
|
42
|
-
try {
|
|
43
|
-
// Check for WebGPU support
|
|
44
|
-
const gpu = navigator.gpu;
|
|
45
|
-
if (!gpu)
|
|
46
|
-
return false;
|
|
47
|
-
// Try to get adapter
|
|
48
|
-
const adapter = await gpu.requestAdapter();
|
|
49
|
-
return adapter !== null;
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Check if transformers.js is available
|
|
57
|
-
*/
|
|
58
|
-
export async function checkTransformersAvailable() {
|
|
59
|
-
try {
|
|
60
|
-
// Try dynamic import - this will fail if package not installed
|
|
61
|
-
await import("@xenova/transformers");
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Browser inference engine using transformers.js
|
|
70
|
-
* STRICT MODE: Throws error if package not installed.
|
|
71
|
-
*/
|
|
72
|
-
export class BrowserInference {
|
|
73
|
-
modelId;
|
|
74
|
-
model = null;
|
|
75
|
-
tokenizer = null;
|
|
76
|
-
pipeline = null; // Keep pipeline for generation
|
|
77
|
-
loadingPromise = null;
|
|
78
|
-
loadError = null;
|
|
79
|
-
statusCallback = null;
|
|
80
|
-
constructor(modelId = "Xenova/distilgpt2", statusCallback = null) {
|
|
81
|
-
this.modelId = modelId;
|
|
82
|
-
this.statusCallback = statusCallback;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Check if browser inference is available
|
|
86
|
-
*/
|
|
87
|
-
async isAvailable() {
|
|
88
|
-
const hasTransformers = await checkTransformersAvailable();
|
|
89
|
-
return hasTransformers;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Load the model (lazy loading)
|
|
93
|
-
* Handles concurrency with loadingPromise
|
|
94
|
-
*/
|
|
95
|
-
async load() {
|
|
96
|
-
if (this.model && this.tokenizer)
|
|
97
|
-
return;
|
|
98
|
-
if (this.loadingPromise)
|
|
99
|
-
return this.loadingPromise;
|
|
100
|
-
if (this.loadError)
|
|
101
|
-
throw this.loadError;
|
|
102
|
-
this.loadingPromise = this.performLoad().finally(() => {
|
|
103
|
-
this.loadingPromise = null;
|
|
104
|
-
});
|
|
105
|
-
return this.loadingPromise;
|
|
106
|
-
}
|
|
107
|
-
async performLoad() {
|
|
108
|
-
if (this.statusCallback)
|
|
109
|
-
this.statusCallback("loading");
|
|
110
|
-
try {
|
|
111
|
-
const { AutoTokenizer, AutoModelForCausalLM, pipeline } = await import("@xenova/transformers");
|
|
112
|
-
const hasWebGPU = await checkWebGPU();
|
|
113
|
-
const device = hasWebGPU ? "webgpu" : "cpu";
|
|
114
|
-
if (this.statusCallback)
|
|
115
|
-
this.statusCallback("loading-tokenizer");
|
|
116
|
-
this.tokenizer = await AutoTokenizer.from_pretrained(this.modelId);
|
|
117
|
-
if (this.statusCallback)
|
|
118
|
-
this.statusCallback("loading-model");
|
|
119
|
-
// biome-ignore lint/suspicious/noExplicitAny: library internal
|
|
120
|
-
this.model = await AutoModelForCausalLM.from_pretrained(this.modelId, {
|
|
121
|
-
device,
|
|
122
|
-
quantized: true,
|
|
123
|
-
});
|
|
124
|
-
if (this.statusCallback)
|
|
125
|
-
this.statusCallback("loading-pipeline");
|
|
126
|
-
// biome-ignore lint/suspicious/noExplicitAny: library internal
|
|
127
|
-
this.pipeline = await pipeline("text-generation", this.modelId, {
|
|
128
|
-
device,
|
|
129
|
-
model: this.model,
|
|
130
|
-
tokenizer: this.tokenizer,
|
|
131
|
-
});
|
|
132
|
-
if (this.statusCallback)
|
|
133
|
-
this.statusCallback("ready");
|
|
134
|
-
}
|
|
135
|
-
catch (err) {
|
|
136
|
-
this.loadError = err instanceof Error ? err : new Error(String(err));
|
|
137
|
-
if (this.statusCallback)
|
|
138
|
-
this.statusCallback("error");
|
|
139
|
-
throw this.loadError;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Generate text
|
|
144
|
-
*/
|
|
145
|
-
async generate(prompt, maxTokens = 50) {
|
|
146
|
-
await this.load();
|
|
147
|
-
if (!this.pipeline) {
|
|
148
|
-
throw new Error("Model not loaded");
|
|
149
|
-
}
|
|
150
|
-
// biome-ignore lint/suspicious/noExplicitAny: library internal
|
|
151
|
-
const result = await this.pipeline(prompt, {
|
|
152
|
-
max_new_tokens: maxTokens,
|
|
153
|
-
temperature: 0.7,
|
|
154
|
-
do_sample: true,
|
|
155
|
-
});
|
|
156
|
-
return result[0]?.generated_text || "";
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Calculate Log-Probability for a given text
|
|
160
|
-
* This is the "Gold Standard" for bias testing.
|
|
161
|
-
* We calculate the loss (negative log likelihood) of the text.
|
|
162
|
-
*/
|
|
163
|
-
async getLogProb(text) {
|
|
164
|
-
await this.load();
|
|
165
|
-
if (!this.tokenizer || !this.model) {
|
|
166
|
-
throw new Error("Model not loaded");
|
|
167
|
-
}
|
|
168
|
-
// biome-ignore lint/suspicious/noExplicitAny: internal transformers.js types
|
|
169
|
-
const inputs = await this.tokenizer(text, { return_tensors: "pt" });
|
|
170
|
-
const inputIds = inputs.input_ids;
|
|
171
|
-
// biome-ignore lint/suspicious/noExplicitAny: internal transformers.js types
|
|
172
|
-
const outputs = await this.model(inputs);
|
|
173
|
-
const logits = outputs.logits;
|
|
174
|
-
// 3. Compute Log Likelihood
|
|
175
|
-
// We want to calculate P(text) = P(t1) * P(t2|t1) * ...
|
|
176
|
-
// LogProb = sum( log( P(ti | t_<i) ) )
|
|
177
|
-
// logits shape: [batch_size, seq_len, vocab_size]
|
|
178
|
-
// inputIds shape: [batch_size, seq_len]
|
|
179
|
-
// We are interested in the probability of the *next* token given previous tokens.
|
|
180
|
-
// So we look at logits[0, i, :] to predict inputIds[0, i+1]
|
|
181
|
-
const data = logits.data; // Float32Array
|
|
182
|
-
const dims = logits.dims; // [batch, seq, vocab]
|
|
183
|
-
const vocabSize = dims[2];
|
|
184
|
-
const seqLen = dims[1];
|
|
185
|
-
let totalLogProb = 0;
|
|
186
|
-
// Helper to get logit value
|
|
187
|
-
const getLogit = (seqIdx, vocabIdx) => {
|
|
188
|
-
return data[seqIdx * vocabSize + vocabIdx];
|
|
189
|
-
};
|
|
190
|
-
// Helper for softmax/logsoftmax
|
|
191
|
-
// We only need the log_prob of the target token
|
|
192
|
-
// log_softmax(x_i) = x_i - log(sum(exp(x_j)))
|
|
193
|
-
// To avoid overflow: x_i - max(x) - log(sum(exp(x_j - max(x))))
|
|
194
|
-
for (let i = 0; i < seqLen - 1; i++) {
|
|
195
|
-
const targetTokenId = Number(inputIds.data[i + 1]);
|
|
196
|
-
// Calculate max logit for numerical stability
|
|
197
|
-
let maxLogit = Number.NEGATIVE_INFINITY;
|
|
198
|
-
for (let j = 0; j < vocabSize; j++) {
|
|
199
|
-
const val = getLogit(i, j);
|
|
200
|
-
if (val > maxLogit)
|
|
201
|
-
maxLogit = val;
|
|
202
|
-
}
|
|
203
|
-
// Calculate sum of exp
|
|
204
|
-
let sumExp = 0;
|
|
205
|
-
for (let j = 0; j < vocabSize; j++) {
|
|
206
|
-
sumExp += Math.exp(getLogit(i, j) - maxLogit);
|
|
207
|
-
}
|
|
208
|
-
const logSumExp = maxLogit + Math.log(sumExp);
|
|
209
|
-
const targetLogit = getLogit(i, targetTokenId);
|
|
210
|
-
const logProb = targetLogit - logSumExp;
|
|
211
|
-
totalLogProb += logProb;
|
|
212
|
-
}
|
|
213
|
-
return totalLogProb;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Get list of supported browser models
|
|
217
|
-
*/
|
|
218
|
-
static getSupportedModels() {
|
|
219
|
-
return BROWSER_MODELS.filter((m) => m.supported);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
8
|
+
export { TransformersInference as BrowserInference, BROWSER_MODELS, checkWebGPU, checkTransformersAvailable, } from "./transformers-inference";
|
|
@@ -4,9 +4,16 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { BiasCalculationMethod } from "../types";
|
|
6
6
|
/**
|
|
7
|
-
* Detection status for model capabilities
|
|
7
|
+
* Detection status for model capabilities.
|
|
8
|
+
*
|
|
9
|
+
* "unverified": the model is declared supported (e.g. it's in the static
|
|
10
|
+
* BROWSER_MODELS list) but has NOT actually been loaded/tested — no request
|
|
11
|
+
* was made, no capability was measured. Distinct from "available", which
|
|
12
|
+
* means a real detection/measurement round confirmed it. Do not report
|
|
13
|
+
* "available" for a model that was never tested (see Messmotor-Sanierung
|
|
14
|
+
* Task 2): the runtime status is what a consumer reads, not source comments.
|
|
8
15
|
*/
|
|
9
|
-
export type CapabilityDetectionStatus = "detecting" | "available" | "unavailable" | "error";
|
|
16
|
+
export type CapabilityDetectionStatus = "detecting" | "available" | "unverified" | "unavailable" | "error";
|
|
10
17
|
/**
|
|
11
18
|
* Model capability information
|
|
12
19
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capability-detection.d.ts","sourceRoot":"","sources":["../../src/inference/capability-detection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEtD
|
|
1
|
+
{"version":3,"file":"capability-detection.d.ts","sourceRoot":"","sources":["../../src/inference/capability-detection.ts"],"names":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