Skip to content

Commit

Permalink
[EdgeTPU] Add EdgeTPUToolchain (#1746)
Browse files Browse the repository at this point in the history
This commit adds EdgeTPUToolchain.

ONE-vscode-DCO-1.0-Signed-off-by: Bumsoo Ko <[email protected]>
  • Loading branch information
dayo09 authored Nov 13, 2023
1 parent 3851a75 commit e5ab694
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 132 deletions.
31 changes: 30 additions & 1 deletion src/Backend/EdgeTPU/EdgeTPUToolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import * as ini from "ini";
import * as fs from "fs";
import * as path from "path";

import { Backend } from "../Backend";
import { Compiler } from "../Compiler";
import { Executor } from "../Executor";
import { Logger } from "../../Utils/Logger";
import { Command } from "../Command";
import { PackageInfo } from "../Toolchain";
Expand Down Expand Up @@ -140,4 +143,30 @@ class EdgeTPUCompiler extends DebianCompiler {
}
}

export { EdgeTPUDebianToolchain, EdgeTPUCompiler };
class EdgeTPUToolchain implements Backend {
private readonly backendName: string;
private readonly toolchainCompiler: Compiler | undefined;

constructor() {
this.backendName = "EdgeTPU";
this.toolchainCompiler = new EdgeTPUCompiler();
}

name(): string {
return this.backendName;
}

compiler(): Compiler | undefined {
return this.toolchainCompiler;
}

executor(): Executor | undefined {
return undefined;
}

executors(): Executor[] {
return [];
}
}

export { EdgeTPUDebianToolchain, EdgeTPUCompiler, EdgeTPUToolchain };
294 changes: 163 additions & 131 deletions src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import * as vscode from "vscode";
import {
EdgeTPUCompiler,
EdgeTPUDebianToolchain,
EdgeTPUToolchain,
} from "../../../Backend/EdgeTPU/EdgeTPUToolchain";
import { ToolchainInfo } from "../../../Backend/Toolchain";
import { Version } from "../../../Backend/Version";
import { TestBuilder } from "../../TestBuilder";

const edgeTPUBackendName = "EdgeTPU";

const content = `
[edgetpu-compile]
input_path=/home/workspace/models/sample.tflite
Expand Down Expand Up @@ -56,10 +59,6 @@ suite("Backend", function () {
testBuilder.setUp();
});

teardown(() => {
testBuilder.tearDown();
});

suite("#run", function () {
test("returns Command with cfg", function () {
testBuilder.writeFileSync("file.cfg", content);
Expand Down Expand Up @@ -119,152 +118,185 @@ suite("Backend", function () {
assert.deepEqual(cmd, expectedStrs);
});
});
});
});

suite("EdgeTPUCompiler", function () {
suite("#constructor()", function () {
test("Create EdgeTPUToolchain compiler", function (pass) {
assert.doesNotThrow(() => new EdgeTPUCompiler());

pass();
assert.ok(true);
teardown(() => {
testBuilder.tearDown();
});
});

suite("#getToolchainTypes", function () {
test("returns EdgeTPUCompiler's type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainTypes = edgeTPUCompiler.getToolchainTypes();
assert.deepEqual(toolchainTypes, ["latest"]);
});
});
suite("EdgeTPUCompiler", function () {
suite("#constructor()", function () {
test("Create EdgeTPUToolchain compiler", function (pass) {
assert.doesNotThrow(() => new EdgeTPUCompiler());

suite("#parseVersion", function () {
test("returns Version object from string version", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("1.0.2~RC0");
const version2 = new Version(1, 0, 2, "~RC0");
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without patch and option", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("16.0");
const version2 = new Version(16, 0, undefined);
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without option", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("2.1.302470888");
const version2 = new Version(2, 1, 302470888);
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without patch", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("1.0-beta");
const version2 = new Version(1, 0, 0, "-beta");
assert.deepEqual(version1, version2);
});
test("NEG: check invalid version format without numbers", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion("a.b.c"));
});
test("NEG: check invalid version format with too many numbers", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion("1.2.3.4"));
});
test("NEG: check invalid version format with empty string", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion(""));
pass();
assert.ok(true);
});
});
});

suite("#getToolchains", function () {
test("get toolchain list", function () {
// No positive test for get toolchain list
// To test this test case, prerequisites() function should be run first.
// However this function needs root permission so it couldn't be executed.
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.isDefined(edgeTPUCompiler.getToolchainTypes);
suite("#getToolchainTypes", function () {
test("returns EdgeTPUCompiler's type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainTypes = edgeTPUCompiler.getToolchainTypes();
assert.deepEqual(toolchainTypes, ["latest"]);
});
});

test("return empty toolchain array when count is 0", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = 0;
const count = 0;
assert.deepStrictEqual(
edgeTPUCompiler.getToolchains(toolchainType, start, count),
[]
);
suite("#parseVersion", function () {
test("returns Version object from string version", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("1.0.2~RC0");
const version2 = new Version(1, 0, 2, "~RC0");
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without patch and option", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("16.0");
const version2 = new Version(16, 0, undefined);
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without option", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("2.1.302470888");
const version2 = new Version(2, 1, 302470888);
assert.deepEqual(version1, version2);
});
test("returns Version object from string version without patch", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const version1 = edgeTPUCompiler.parseVersion("1.0-beta");
const version2 = new Version(1, 0, 0, "-beta");
assert.deepEqual(version1, version2);
});
test("NEG: check invalid version format without numbers", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion("a.b.c"));
});
test("NEG: check invalid version format with too many numbers", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion("1.2.3.4"));
});
test("NEG: check invalid version format with empty string", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.throws(() => edgeTPUCompiler.parseVersion(""));
});
});

test("NEG: request wrong toolchain type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const dummyToolchainType = "dummy";
const start = 0;
const count = 1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(dummyToolchainType, start, count)
);
});
suite("#getToolchains", function () {
test("get toolchain list", function () {
// No positive test for get toolchain list
// To test this test case, prerequisites() function should be run first.
// However this function needs root permission so it couldn't be executed.
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.isDefined(edgeTPUCompiler.getToolchainTypes);
});

test("NEG: request wrong start number", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = -1;
const count = 1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(toolchainType, start, count)
);
});
test("return empty toolchain array when count is 0", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = 0;
const count = 0;
assert.deepStrictEqual(
edgeTPUCompiler.getToolchains(toolchainType, start, count),
[]
);
});

test("NEG: request wrong count number", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = 0;
const count = -1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(toolchainType, start, count)
);
test("NEG: request wrong toolchain type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const dummyToolchainType = "dummy";
const start = 0;
const count = 1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(dummyToolchainType, start, count)
);
});

test("NEG: request wrong start number", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = -1;
const count = 1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(toolchainType, start, count)
);
});

test("NEG: request wrong count number", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const toolchainType = "latest";
const start = 0;
const count = -1;
assert.throws(() =>
edgeTPUCompiler.getToolchains(toolchainType, start, count)
);
});
});
});

suite("#getInstalledToolchains", function () {
test("get toolchain list", function () {
// No positive test for get toolchain list
// To test this test case, prerequisites() function should be run first.
// However this function needs root permission so it couldn't be executed.
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.isDefined(edgeTPUCompiler.getInstalledToolchains);
suite("#getInstalledToolchains", function () {
test("get toolchain list", function () {
// No positive test for get toolchain list
// To test this test case, prerequisites() function should be run first.
// However this function needs root permission so it couldn't be executed.
const edgeTPUCompiler = new EdgeTPUCompiler();
assert.isDefined(edgeTPUCompiler.getInstalledToolchains);
});

test("NEG: request wrong toolchain type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const dummyToolchainType = "dummy";
assert.throws(() =>
edgeTPUCompiler.getInstalledToolchains(dummyToolchainType)
);
});
});

test("NEG: request wrong toolchain type", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const dummyToolchainType = "dummy";
assert.throws(() =>
edgeTPUCompiler.getInstalledToolchains(dummyToolchainType)
);
suite("#prerequisitesForGetToolchains", function () {
test("returns a command which executes a shell script for prerequisites", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const extensionId = "Samsung.one-vscode";
const ext = vscode.extensions.getExtension(
extensionId
) as vscode.Extension<any>;
const scriptPath = vscode.Uri.joinPath(
ext!.extensionUri,
"script",
"prerequisitesForGetEdgeTPUToolchain.sh"
).fsPath;
const cmd = `sudo /bin/sh ${scriptPath}`;
assert.deepStrictEqual(
edgeTPUCompiler.prerequisitesForGetToolchains().str(),
cmd
);
});
});
});

suite("#prerequisitesForGetToolchains", function () {
test("returns a command which executes a shell script for prerequisites", function () {
const edgeTPUCompiler = new EdgeTPUCompiler();
const extensionId = "Samsung.one-vscode";
const ext = vscode.extensions.getExtension(
extensionId
) as vscode.Extension<any>;
const scriptPath = vscode.Uri.joinPath(
ext!.extensionUri,
"script",
"prerequisitesForGetEdgeTPUToolchain.sh"
).fsPath;
const cmd = `sudo /bin/sh ${scriptPath}`;
assert.deepStrictEqual(
edgeTPUCompiler.prerequisitesForGetToolchains().str(),
cmd
);
suite("EdgeTPUToolchain", function () {
suite("#constructor()", function () {
test("Create dummy EdgeTPUToolchain backend", function (pass) {
assert.doesNotThrow(() => new EdgeTPUToolchain());

pass();
assert.ok(true);
});
});

suite("#name()", function () {
test("returns backend name", function () {
const edgeTPUBackend = new EdgeTPUToolchain();
assert.strictEqual(edgeTPUBackend.name(), edgeTPUBackendName);
});
});

suite("#compiler()", function () {
test("returns edgeTPUCompiler", function () {
const edgeTPUBackend = new EdgeTPUToolchain();
const edgeTPUCompiler = edgeTPUBackend.compiler();
assert.instanceOf(edgeTPUCompiler, EdgeTPUCompiler);
});
});

// TODO
// Add test case for executor() and executors()
});
});
});

0 comments on commit e5ab694

Please sign in to comment.