Skip to content

Commit

Permalink
[EdgeTPU] Add EdgeTPU backend registration codes (Samsung#1716)
Browse files Browse the repository at this point in the history
This commit modify the testing codes to test registration of EdgeTPUToolchain.
Also, This commit exports BackendMap interface to use it in the testing codes.

ONE-vscode-DCO-1.0-Signed-off-by: Bumsoo Ko <[email protected]>
ONE-vscode-DCO-1.0-Signed-off-by: profornnan <[email protected]>
  • Loading branch information
rhqjatn2398 authored Oct 19, 2023
1 parent 1007926 commit 0b9640e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/Backend/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Logger } from "../Utils/Logger";
* Interface of backend map
* - Use Object class to use the only string key
*/
interface BackendMap {
export interface BackendMap {
[key: string]: Backend;
}

Expand Down
52 changes: 24 additions & 28 deletions src/Tests/Backend/Backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

import { assert } from "chai";

import { API, globalBackendMap } from "../../Backend/API";
import { API, BackendMap, globalBackendMap } from "../../Backend/API";
import { gToolchainEnvMap } from "../../Toolchain/ToolchainEnv";
import { Backend } from "../../Backend/Backend";
import { Compiler, CompilerBase } from "../../Backend/Compiler";
import { Executor, ExecutorBase } from "../../Backend/Executor";
import { OneToolchain } from "../../Backend/One/OneToolchain";
import { gToolchainEnvMap } from "../../Toolchain/ToolchainEnv";
import { EdgeTPUToolchain } from "../../Backend/EdgeTPU/EdgeTPUToolchain";

const oneBackendName = "ONE";
const edgeTPUBackendName = "EdgeTPU";

// TODO: Move it to Mockup
const backendName = "Mockup";
Expand All @@ -47,45 +49,39 @@ class BackendMockup implements Backend {
}
}

const expectedGlobalBackendMap: BackendMap = {};
expectedGlobalBackendMap[oneBackendName] = new OneToolchain();
expectedGlobalBackendMap[edgeTPUBackendName] = new EdgeTPUToolchain();

suite("Backend", function () {
suite("backendAPI", function () {
test("registers a OneToolchain", function () {
let oneBackend = new OneToolchain();
assert.strictEqual(Object.entries(globalBackendMap).length, 1);

const entries = Object.entries(globalBackendMap);
assert.strictEqual(entries.length, 1);
// this runs once
for (const [key, value] of entries) {
assert.strictEqual(key, oneBackendName);
assert.deepStrictEqual(value, oneBackend);
}
assert.strictEqual(entries.length, 2);

assert.deepStrictEqual(globalBackendMap, expectedGlobalBackendMap);
});
test("registers a backend", function () {
assert.strictEqual(Object.entries(globalBackendMap).length, 1);
assert.strictEqual(Object.entries(globalBackendMap).length, 2);

let backend = new BackendMockup();
API.registerBackend(backend);

const entries = Object.entries(globalBackendMap);
assert.strictEqual(entries.length, 2);
assert.strictEqual(entries.length, 3);

// this runs once
for (const [key, value] of entries) {
if (key !== oneBackendName) {
assert.strictEqual(key, backendName);
assert.deepStrictEqual(value, backend);
}
assert.deepStrictEqual(backend, globalBackendMap[backend.name()]);

if (gToolchainEnvMap[backend.name()] !== undefined) {
delete gToolchainEnvMap[backend.name()];
}
});
});

teardown(function () {
if (globalBackendMap[backendName] !== undefined) {
delete globalBackendMap[backendName];
}
if (gToolchainEnvMap[backendName] !== undefined) {
delete gToolchainEnvMap[backendName];
}
if (globalBackendMap[backend.name()] !== undefined) {
delete globalBackendMap[backend.name()];
}

assert.isUndefined(gToolchainEnvMap[backend.name()]);
assert.isUndefined(globalBackendMap[backend.name()]);
});
});
});
68 changes: 38 additions & 30 deletions src/Tests/Toolchain/ToolchainProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,15 @@ import {

suite("Toolchain", function () {
const oneBackendName = "ONE";
const edgeTPUBackendName = "EdgeTPU";
const backendName = "dummy_backend";
const compiler = new MockCompiler();
const toolchainEnv = new ToolchainEnv(compiler);
const backendName = "dummy_backend";

setup(function () {
gToolchainEnvMap[backendName] = toolchainEnv;
});

teardown(function () {
if (gToolchainEnvMap[backendName] !== undefined) {
delete gToolchainEnvMap[backendName];
}
});

suite("BaseNode", function () {
suite("#constructor()", function () {
test("is constructed with params using base_node", function () {
Expand Down Expand Up @@ -115,20 +110,22 @@ suite("Toolchain", function () {
suite("#createBackendNodes()", function () {
test("creates BackendNode list", function () {
let bnodes: BackendNode[] = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
});
});
suite("#createToolchainNodes()", function () {
test("creates ToolchainNode list", function () {
let bnodes: BackendNode[] = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);

// Ignore bnodes[0] because it is ONE Toolchain backend.
let bnode2: BackendNode = bnodes[1];
// Ignore bnodes[0], bnodes[1] because these are ONE Toolchain backend and EdgeTPU Toolchain backend.
let bnode2: BackendNode = bnodes[2];
let tnodes2 = NodeBuilder.createToolchainNodes(bnode2);
assert.strictEqual(tnodes2.length, 1);
tnodes2.forEach((tnode) => {
Expand All @@ -139,10 +136,11 @@ suite("Toolchain", function () {
suite("#createToolchainNodes()", function () {
test("NEG: creates ToolchainNode list using invalid backend node", function () {
const bnodes: BackendNode[] = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
const tnodes1 = NodeBuilder.createToolchainNodes(bnodes[1]);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
const tnodes1 = NodeBuilder.createToolchainNodes(bnodes[2]);
assert.strictEqual(tnodes1.length, 1);
tnodes1.forEach((tnode) => {
assert.strictEqual(tnode.backendName, backendName);
Expand Down Expand Up @@ -196,20 +194,22 @@ suite("Toolchain", function () {
test("gets Children with undefined", function (done) {
let provider = new ToolchainProvider();
provider.getChildren(undefined).then((bnodes) => {
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
done();
});
});
test("gets Children with BackendNode", function (done) {
let provider = new ToolchainProvider();
let bnodes: BackendNode[] = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
// Ignore bnodes[0] because it is ONE Toolchain backend.
let bnode: BackendNode = bnodes[1];
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
// Ignore bnodes[0], bnodes[1] because these are ONE Toolchain backend and EdgeTPU Toolchain backend.
let bnode: BackendNode = bnodes[2];
provider.getChildren(bnode).then((tnodes) => {
assert.strictEqual(tnodes.length, 1);
tnodes.forEach((tnode) => {
Expand Down Expand Up @@ -255,11 +255,12 @@ suite("Toolchain", function () {
test("requests uninstall", function () {
const provider = new ToolchainProvider();
const bnodes = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
// Ignore bnodes[0] because it is ONE Toolchain backend.
const tnodes = NodeBuilder.createToolchainNodes(bnodes[1]);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
// Ignore bnodes[0], bnodes[1] because these are ONE Toolchain backend and EdgeTPU Toolchain backend.
const tnodes = NodeBuilder.createToolchainNodes(bnodes[2]);
assert.isAbove(tnodes.length, 0);
provider.uninstall(tnodes[0]);
assert.isTrue(true);
Expand Down Expand Up @@ -336,11 +337,12 @@ suite("Toolchain", function () {
test("request setDefaultToolchain", function () {
const provider = new ToolchainProvider();
const bnodes = NodeBuilder.createBackendNodes();
assert.strictEqual(bnodes.length, 2);
assert.strictEqual(bnodes.length, 3);
assert.strictEqual(bnodes[0].label, oneBackendName);
assert.strictEqual(bnodes[1].label, backendName);
// Ignore bnodes[0] because it is ONE Toolchain backend.
const tnodes = NodeBuilder.createToolchainNodes(bnodes[1]);
assert.strictEqual(bnodes[1].label, edgeTPUBackendName);
assert.strictEqual(bnodes[2].label, backendName);
// Ignore bnodes[0], bnodes[1] because these are ONE Toolchain backend, EdgeTPU Toolchain backend.
const tnodes = NodeBuilder.createToolchainNodes(bnodes[2]);
assert.isAbove(tnodes.length, 0);
provider.setDefaultToolchain(tnodes[0]);
assert.isTrue(
Expand All @@ -366,4 +368,10 @@ suite("Toolchain", function () {
});
});
});

teardown(function () {
if (gToolchainEnvMap[backendName] !== undefined) {
delete gToolchainEnvMap[backendName];
}
});
});
22 changes: 12 additions & 10 deletions src/Tests/View/InstallQuickInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,18 @@ suite("View", function () {
// Therefore, we focus on testing things not ui
suite("InstallQuickInput", function () {
const oneBackendName = "ONE";
const edgeTPUBackendName = "EdgeTPU";
const backendName = "testBackend";
const compiler = new MockCompiler();
const toolchainEnv = new ToolchainEnv(compiler);
const toolchainType = toolchainEnv.getToolchainTypes()[0];
const toolchain = toolchainEnv.listAvailable(toolchainType, 0, 1)[0];
const version = new Version(1, 0, 0).str();
const backendName = "testBackend";


setup(function () {
gToolchainEnvMap[backendName] = toolchainEnv;
});

teardown(function () {
if (gToolchainEnvMap[backendName] !== undefined) {
delete gToolchainEnvMap[backendName];
}
});

suite("#constructor()", function () {
test("is constructed", function () {
let quickInput = new InstallQuickInput();
Expand Down Expand Up @@ -372,9 +367,10 @@ suite("View", function () {
test("gets all toolchain env names from global toolchain env", function () {
let quickInput = new InstallQuickInput();
let envs = quickInput.getAllToolchainEnvNames();
assert.strictEqual(envs.length, 2);
assert.strictEqual(envs.length, 3);
assert.strictEqual(envs[0], oneBackendName);
assert.strictEqual(envs[1], backendName);
assert.strictEqual(envs[1], edgeTPUBackendName);
assert.strictEqual(envs[2], backendName);
});
});

Expand Down Expand Up @@ -666,5 +662,11 @@ suite("View", function () {
}).to.throw(`state is wrong: ` + String(invalidState.current));
});
});

teardown(function () {
if (gToolchainEnvMap[backendName] !== undefined) {
delete gToolchainEnvMap[backendName];
}
});
});
});
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { Logger } from "./Utils/Logger";
import { VisqViewerProvider } from "./Visquv/VisqViewer";
import { MPQEditorProvider } from "./MPQEditor/MPQEditor";
import { MPQSelectionPanel } from "./MPQEditor/MPQCircleSelector";
import { EdgeTPUToolchain } from "./Backend/EdgeTPU/EdgeTPUToolchain";

/* istanbul ignore next */
export function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -87,6 +88,7 @@ export function activate(context: vscode.ExtensionContext) {
MPQSelectionPanel.register(context);

API.registerBackend(new OneToolchain());
API.registerBackend(new EdgeTPUToolchain());

// returning backend registration function that will be called by backend extensions
return API;
Expand Down

0 comments on commit 0b9640e

Please sign in to comment.