File size: 3,649 Bytes
79ae023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Typed fetch wrappers for the Vitis acceleration layer API (/api/vitis). */
import type {
  AugmentBatch,
  AugmentationSpec,
  BackendStatus,
  HilRun,
  HilSweep,
  IspRun,
  PrdDoc,
  PrdListEntry,
  RunListEntry,
  TemporalRun,
} from '../types/vitis';

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    let detail = '';
    try {
      const body = await res.json();
      detail = typeof body?.detail === 'string' ? body.detail : JSON.stringify(body);
    } catch {
      detail = await res.text().catch(() => '');
    }
    const err = new Error(detail || res.statusText) as Error & { status?: number };
    err.status = res.status;
    throw err;
  }
  return res.json() as Promise<T>;
}

const JSON_HEADERS = { 'Content-Type': 'application/json' };

const get = <T,>(url: string): Promise<T> => fetch(url).then((r) => handle<T>(r));
const post = <T,>(url: string, body?: unknown): Promise<T> =>
  fetch(url, {
    method: 'POST',
    headers: JSON_HEADERS,
    body: body === undefined ? undefined : JSON.stringify(body),
  }).then((r) => handle<T>(r));

// ------------------------------------------------------------- backends

export const getBackendStatus = () => get<BackendStatus>('/api/vitis/backends/status');

// ------------------------------------------------------------- HIL

export interface HilRunRequest {
  n_sequences?: number;
  frames_per_sequence?: number;
  seed?: number;
  width_bits?: number;
  int_bits?: number;
  max_line_buffer_depth?: number;
  use_lut_approx?: boolean;
  lut_bits?: number;
  device?: string;
  regression_delta?: number;
  alpha?: number;
  run_ablation?: boolean;
}

export const runHil = (body: HilRunRequest) => post<HilRun>('/api/vitis/hil/run', body);

export const runHilSweep = (body: HilRunRequest & { widths?: number[] }) =>
  post<HilSweep>('/api/vitis/hil/sweep', body);

export const listHilRuns = () => get<{ runs: RunListEntry[] }>('/api/vitis/hil/runs');

// ------------------------------------------------------------- ISP

export interface IspRunRequest {
  n_frames?: number;
  seed?: number;
  stages?: string[];
  stage_params?: Record<string, unknown>;
  width_bits?: number;
  int_bits?: number;
  max_line_buffer_depth?: number;
  use_lut_approx?: boolean;
  device?: string;
  include_previews?: boolean;
}

export const runIsp = (body: IspRunRequest) => post<IspRun>('/api/vitis/isp/run', body);

export const getIspStages = () => get<{ stages: string[] }>('/api/vitis/isp/stages');

// ------------------------------------------------------------- augment

export const getAugmentRecipes = () =>
  get<{ augmentations: AugmentationSpec[] }>('/api/vitis/augment/recipes');

export const generateAugmentBatch = (body: {
  recipes?: { aug: string; params?: Record<string, number> }[];
  n_variants?: number;
  seed?: number;
  backend?: string;
  width_bits?: number;
  device?: string;
  include_thumbnails?: boolean;
}) => post<AugmentBatch>('/api/vitis/augment/generate', body);

// ------------------------------------------------------------- temporal

export const getTemporalEngines = () =>
  get<{ engines: string[] }>('/api/vitis/temporal/engines');

export const runTemporal = (body: {
  engines?: string[];
  n_sequences?: number;
  frames_per_sequence?: number;
  seed?: number;
  width_bits?: number;
  device?: string;
}) => post<TemporalRun>('/api/vitis/temporal/run', body);

// ------------------------------------------------------------- PRD

export const listPrds = () => get<{ prds: PrdListEntry[] }>('/api/vitis/prd');

export const getPrd = (id: string) =>
  get<PrdDoc>(`/api/vitis/prd/${encodeURIComponent(id)}`);