Skip to content

Commit 300768e

Browse files
Add documentation for the case cache (#3178)
Add `npm run node` helper to sit alongside `npm run standalone` and `npm run wpt`. Update build.md for `out-node`. Co-authored-by: Kai Ninomiya <[email protected]>
1 parent 8ee974a commit 300768e

File tree

3 files changed

+116
-16
lines changed

3 files changed

+116
-16
lines changed

docs/build.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
# Building
22

33
Building the project is not usually needed for local development.
4-
However, for exports to WPT, or deployment (https://gpuweb.github.io/cts/),
4+
However, for exports to WPT, NodeJS, [or deployment](https://gpuweb.github.io/cts/),
55
files can be pre-generated.
66

7-
The project builds into two directories:
7+
## Build types
88

9-
- `out/`: Built framework and test files, needed to run standalone or command line.
10-
- `out-wpt/`: Build directory for export into WPT. Contains:
11-
- An adapter for running WebGPU CTS tests under WPT
12-
- A copy of the needed files from `out/`
13-
- A copy of any `.html` test cases from `src/`
9+
The project can be built several different ways, each with a different output directory:
10+
11+
### 0. on-the-fly builds (no output directory)
12+
13+
Use `npm run start` to launch a server that live-compiles everything as needed.
14+
Use `tools/run_node` and other tools to run under `ts-node` which compiles at runtime.
15+
16+
### 1. `out` directory
17+
18+
**Built with**: `npm run standalone`
19+
20+
**Serve locally with**: `npx grunt serve`
21+
22+
**Used for**: Static deployment of the CTS, primarily for [gpuweb.github.io/cts](https://gpuweb.github.io/cts/).
23+
24+
### 2. `out-wpt` directory
25+
26+
**Built with**: `npm run wpt`
27+
28+
**Used for**: Deploying into [Web Platform Tests](https://web-platform-tests.org/). See [below](#export-to-wpt) for more information.
29+
30+
Contains:
31+
32+
- An adapter for running WebGPU CTS tests under WPT
33+
- A copy of the needed files from `out/`
34+
- A copy of any `.html` test cases from `src/`
35+
36+
### 3. `out-node` directory
37+
38+
**Built with**: `npm run node`
39+
40+
**Used for**: Running NodeJS tools, if you want to specifically avoid the live-compilation overhead of the `tools/` versions, or are running on a deployment which no longer has access to `ts-node` (which is a build-time dependency). For example:
41+
42+
- `node out-node/common/runtime/cmdline.js` ([source](../src/common/runtime/cmdline.ts)) - A command line interface test runner
43+
- `node out-node/common/runtime/server.js` ([source](../src/common/runtime/server.ts)) - An HTTP server for executing CTS tests with a REST interface
44+
45+
## Testing
1446

1547
To build and run all pre-submit checks (including type and lint checks and
1648
unittests), use:
@@ -25,15 +57,6 @@ For checks only:
2557
npm run check
2658
```
2759

28-
For a quicker iterative build:
29-
30-
```sh
31-
npm run standalone
32-
```
33-
34-
## Run
35-
36-
To serve the built files (rather than using the dev server), run `npx grunt serve`.
3760

3861
## Export to WPT
3962

docs/case_cache.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Case Cache
2+
3+
The WebGPU CTS contains many tests that check that the results of an operation
4+
fall within limits defined by the WebGPU and WGSL specifications. The
5+
computation of these allowed limits can be very expensive to calculate, however
6+
the values do not vary by platform or device, and can be precomputed and reused
7+
for multiple CTS runs.
8+
9+
## File cache
10+
11+
To speed up execution of the CTS, the CTS git repo holds holds pre-computed
12+
test cases, serialized in a set of gzip-compressed binary files under
13+
[`src/resources/cache`](../src/resources/cache).
14+
15+
These files are regenerated by [`src/common/tools/gen_cache.ts`](../src/common/tools/gen_cache.ts)
16+
which can be run with `npx grunt run:generate-cache`.
17+
This tool is automatically run by the various Grunt build commands.
18+
19+
As generating the cache is expensive (hence why we build it ahead of time!) the
20+
cache generation tool will only re-build the cache files it believes may be out
21+
of date. To determine which files it needs to rebuild, the tool calculates a
22+
hash of all the transitive source TypeScript files that are used to build the
23+
output, and compares this hash to the hash stored in
24+
[`src/resources/cache/hashes.json`](`../src/resources/cache/hashes.json`). Only
25+
those cache files with differing hashes are rebuilt.
26+
27+
Since source changes will sometimes change the hash without changing the generated cache,
28+
sometimes the cache will be regenerated unnecessarily. **This is OK, but try to avoid committing
29+
no-op regenerations - this will happen if your version of Node produces different gzip outputs
30+
than the original committer's Node did for the same input.**
31+
32+
The cache files are copied from [`src/resources/cache`](../src/resources/cache)
33+
to the `resources/cache` subdirectory of the
34+
[`out` and `out-node` build directories](build.md#build-types), so the runner
35+
can load these cache files.
36+
37+
The GitHub presubmit checks will error if the cache files or
38+
[`hashes.json`](`../src/resources/cache/hashes.json`) need updating.
39+
40+
## In memory cache
41+
42+
If a cache file cannot be found, then the [`CaseCache`](../src/webgpu/shader/execution/expression/case_cache.ts)
43+
will build the cases during CTS execution and store the results in an in-memory LRU cache.
44+
45+
## Using the cache
46+
47+
To add test cases to the cache:
48+
49+
1. Import `makeCaseCache` from [`'case_cache.js'`](../src/webgpu/shader/execution/expression/case_cache.ts);
50+
51+
```ts
52+
import { makeCaseCache } from '../case_cache.js'; // your relative path may vary
53+
```
54+
55+
2. Declare an exported global variable with the name `d`, assigned with the return value of `makeCaseCache()`:
56+
57+
```ts
58+
export const d = makeCaseCache('unique/path/of/your/cache/file', {
59+
// Declare any number of fields that build the test cases
60+
name_of_your_case: () => {
61+
return fullI32Range().map(e => { // example case builder
62+
return { input: i32(e), expected: i32(-e) };
63+
});
64+
},
65+
});
66+
```
67+
68+
3. To load the cases from the cache, use `d.get();`
69+
70+
```ts
71+
const cases = await d.get('name_of_your_case');
72+
// cases will either be loaded from the cache file, loaded from the in-memory
73+
// LRU, or built on the fly.
74+
```
75+
76+
4. Run `npx grunt run generate-cache` to generate the new cache file.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"all": "grunt all",
88
"standalone": "grunt standalone",
99
"wpt": "grunt wpt",
10+
"node": "grunt node",
1011
"checks": "grunt checks",
1112
"unittest": "grunt unittest",
1213
"typecheck": "grunt typecheck",

0 commit comments

Comments
 (0)