Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

Merge main into this branch and resolve conflicts

Nandakishorda471cbd82c8
diff --cc laya-ts/src/router.ts
index 87fccffb,c307e013..0f09b078
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@@ -198,50 -215,44 +216,55 @@@ export class Router extends HookRegistr
this._touch(key);
return this._agents.get(key);
}
- let agent: unknown;
- if (this.loader) {
- agent = await this.loader(key, this.models[key]);
- } else {
- const { Agent } = await import("./agent.js");
- const spec = this.models[key];
- const revision = Object.prototype.hasOwnProperty.call(this.revisions, key)
- ? this.revisions[key]
- : this.revision;
- const opts: Record<string, unknown> = {
- subfolder: spec.subfolder,
- device: this.device ?? undefined,
- token: this.token ?? undefined,
- };
- if (revision) opts.revision = revision;
- agent = await (Agent as unknown as {
- load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
- }).load(spec.repo, opts);
- }
- this._agents.set(key, agent);
- this._order.push(key);
- const evicted = this._evict();
- // Lifecycle hooks fire after the maps settle, so a hook can safely call the Router.
- for (const victim of evicted) {
+ const loading = this._loading.get(key);
+ if (loading) return loading;
+ // Start in a microtask so even a synchronous loader sees its in-flight entry.
+ const pending = Promise.resolve().then(async () => {
+ let agent: unknown;
+ if (this.loader) {
+ agent = await this.loader(key, this.models[key]);
+ } else {
+ const { Agent } = await import("./agent.js");
+ const spec = this.models[key];
- agent = await (Agent as unknown as {
- load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
- }).load(spec.repo, {
++ const revision = Object.prototype.hasOwnProperty.call(this.revisions, key)
++ ? this.revisions[key]
++ : this.revision;
++ const opts: Record<string, unknown> = {
+ subfolder: spec.subfolder,
+ device: this.device ?? undefined,
+ token: this.token ?? undefined,
- });
++ };
++ if (revision) opts.revision = revision;
++ agent = await (Agent as unknown as {
++ load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
++ }).load(spec.repo, opts);
+ }
+ this._agents.set(key, agent);
+ this._order.push(key);
+ const evicted = this._evict();
+ // Lifecycle hooks fire after the maps settle, so a hook can safely call the Router.
+ for (const victim of evicted) {
+ dispatch(
+ composeHooks(this.hooks),
+ "onEvict",
+ new PredictContext({ states: [], questions: {}, model: victim, router: this }),
+ { raiseErrors: this.hooksRaise },
+ );
+ }
dispatch(
composeHooks(this.hooks),
- "onEvict",
- new PredictContext({ states: [], questions: {}, model: victim, router: this }),
+ "onLoad",
+ new PredictContext({ states: [], questions: {}, model: key, agent, router: this }),
{ raiseErrors: this.hooksRaise },
);
+ return agent;
+ });
+ this._loading.set(key, pending);
+ try {
+ return await pending;
+ } finally {
+ this._loading.delete(key);
}
- dispatch(
- composeHooks(this.hooks),
- "onLoad",
- new PredictContext({ states: [], questions: {}, model: key, agent, router: this }),
- { raiseErrors: this.hooksRaise },
- );
- return agent;
}
_touch(key: string): void {