Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a custom sort function #123

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/src/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export const fn = <const P extends readonly any[], const R>(
};

/** Construct an opaque function whose implementation runs `f`. */
export const opaque = <const P extends readonly Reals[], const R extends Reals>(
export const opaque = <const P extends readonly any[], const R>(
params: P,
ret: R,
f: (...args: JsArgs<SymbolicParams<P>>) => ToJs<Value<R>>,
Expand Down Expand Up @@ -604,6 +604,8 @@ type ToJs<T> = [T] extends [Null]
? number
: [T] extends [Nat]
? number
: [T] extends [Vec<infer V>]
? ToJs<V>[]
: { [K in keyof T]: ToJs<T[K]> };

/** Map from an abstract value type array to a concrete argument type array. */
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,46 @@ describe("valid", () => {
const h = await compile(g);
expect(h({ v: [2], i: 0 })).toEqual({ v: [1], i: 0 });
});

test("sort", async () => {
const n = 3;

const sortWithIndices = opaque(
[Vec(n, Real)],
Vec(n, struct({ x: Real, i: n })),
(v) => v.map((x, i) => ({ x, i })).sort((a, b) => a.x - b.x),
);
sortWithIndices.jvp = fn([Vec(n, Dual)], Vec(n, { x: Dual, i: n }), (v) => {
const w = sortWithIndices(vec(n, Real, (i) => v[i].re));
return vec(n, { x: Dual, i: n }, (j) => {
const { i } = w[j];
return { x: v[i], i };
});
});

const sort = fn([Vec(n, Real)], Vec(n, Real), (v) => {
const w = sortWithIndices(v);
return vec(n, Real, (i) => w[i].x);
});

const sortGrad = fn([Vec(n, Real), Vec(n, Real)], Vec(n, Real), (v, g) =>
vjp(sort)(v).grad(g),
);

const interpreted = interp(sortGrad);
expect(interpreted([42, 121, 342], [-1, -2, -3])).toEqual([-1, -2, -3]);
expect(interpreted([42, 342, 121], [-1, -2, -3])).toEqual([-1, -3, -2]);
expect(interpreted([121, 42, 342], [-1, -2, -3])).toEqual([-2, -1, -3]);
expect(interpreted([121, 342, 42], [-1, -2, -3])).toEqual([-2, -3, -1]);
expect(interpreted([342, 42, 121], [-1, -2, -3])).toEqual([-3, -1, -2]);
expect(interpreted([342, 121, 42], [-1, -2, -3])).toEqual([-3, -2, -1]);

const compiled = await compile(sortGrad);
expect(compiled([42, 121, 342], [-1, -2, -3])).toEqual([-1, -2, -3]);
expect(compiled([42, 342, 121], [-1, -2, -3])).toEqual([-1, -3, -2]);
expect(compiled([121, 42, 342], [-1, -2, -3])).toEqual([-2, -1, -3]);
expect(compiled([121, 342, 42], [-1, -2, -3])).toEqual([-2, -3, -1]);
expect(compiled([342, 42, 121], [-1, -2, -3])).toEqual([-3, -1, -2]);
expect(compiled([342, 121, 42], [-1, -2, -3])).toEqual([-3, -2, -1]);
});
});
Loading