File size: 3,488 Bytes
1cc8d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type {
  CheckpointId,
  PrefixHash,
  ProfileId,
  ProposalId,
  RequestId,
  SessionEpoch,
} from './ids.ts';

export interface EngineIdentity {
  modelHash: string;
  tokenizerHash: string;
  templateHash: string;
  numericalProfile: ProfileId;
  stateSchema: number;
}

export interface PrefixState {
  processedLength: number;
  prefixHash: PrefixHash;
  pendingToken?: number;
  samplerPhase: 'beforeDraw' | 'afterDraw';
}

export interface CheckpointRef {
  id: CheckpointId;
  identity: EngineIdentity;
  prefix: PrefixState;
  canResampleNext: boolean;
}

export interface GenerateRequest {
  epoch: SessionEpoch;
  requestId: RequestId;
  samplerProfile: ProfileId;
  seed: bigint;
  maxNewTokens: number;
  stopPolicy: string;
}

export type StopReason = 'eos' | 'length' | 'cancelled' | 'tool' | 'grammar' | 'fault' | 'window';

export type EngineEvent =
  | { type: 'preparation'; requestId: RequestId; stage: PrepareStage; completed: number; total: number }
  | { type: 'prefill'; requestId: RequestId; processed: number; total: number }
  | { type: 'token'; requestId: RequestId; tokenId: number; index: number }
  | {
      type: 'channel';
      requestId: RequestId;
      channel: 'reasoning' | 'answer';
      text: string;
      tokenStart: number;
      tokenEnd: number;
    }
  | { type: 'toolProposal'; requestId: RequestId; proposal: ToolProposal }
  | { type: 'completion'; requestId: RequestId; reason: StopReason; processed: number; emitted: number }
  | { type: 'cancelled'; requestId: RequestId; boundary: PrefixState }
  | { type: 'error'; requestId: RequestId | null; recoverable: boolean; code: string; message: string }
  | { type: 'checkpoint'; requestId: RequestId; checkpoint: CheckpointRef };

export type PrepareStage =
  | 'manifest'
  | 'download'
  | 'verify'
  | 'upload'
  | 'compile'
  | 'calibrate'
  | 'ready';

export interface ToolCall {
  name: string;
  arguments: Record<string, unknown>;
}

export interface ToolProposal {
  proposalId: ProposalId;
  call: ToolCall;
  rawText: string;
  complete: boolean;
  parseError?: string;
}

export interface InferenceEngine {
  load(manifestUrl: string): Promise<EngineIdentity>;
  prefill(tokens: Uint32Array): Promise<PrefixState>;
  generate(req: GenerateRequest): AsyncIterable<EngineEvent>;
  checkpoint(): Promise<CheckpointRef>;
  restore(ref: CheckpointRef): Promise<PrefixState>;
  cancel(requestId: number): Promise<PrefixState>;
  dispose(): Promise<void>;
}

export interface DeviceCapabilities {
  webgpu: boolean;
  secureContext: boolean;
  crossOriginIsolated: boolean;
  adapter: AdapterReport | null;
  features: string[];
  wgslLanguageFeatures: string[];
  limits: Record<string, number>;
  timestampQuery: boolean;
  subgroups: boolean;
  subgroupSizeControl: boolean;
  immediateAddressSpace: boolean;
  bufferView: boolean;
  subgroupUniformity: boolean;
  subgroupId: boolean;
  shaderF16: boolean;
  opfs: boolean;
  indexedDb: boolean;
  sharedArrayBuffer: boolean;
  webLocks: boolean;
  broadcastChannel: boolean;
  serviceWorker: boolean;
  screenWakeLock: boolean;
  deviceMemoryGiB: number | null;
  storageQuotaBytes: number | null;
  storageUsageBytes: number | null;
}

export interface AdapterReport {
  vendor: string;
  architecture: string;
  device: string;
  description: string;
  subgroupMinSize: number;
  subgroupMaxSize: number;
  isFallbackAdapter: boolean;
}

export interface CapabilityFailure {
  missing: string[];
  detail: string;
}