From e5ab6944fe0a9d8b9e3fc7b8a8ca1585ea878796 Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Mon, 13 Nov 2023 15:25:17 +0900 Subject: [PATCH] [EdgeTPU] Add EdgeTPUToolchain (#1746) This commit adds EdgeTPUToolchain. ONE-vscode-DCO-1.0-Signed-off-by: Bumsoo Ko --- src/Backend/EdgeTPU/EdgeTPUToolchain.ts | 31 +- .../Backend/EdgeTPU/EdgeTPUToolchain.test.ts | 294 ++++++++++-------- 2 files changed, 193 insertions(+), 132 deletions(-) diff --git a/src/Backend/EdgeTPU/EdgeTPUToolchain.ts b/src/Backend/EdgeTPU/EdgeTPUToolchain.ts index 1c7f95ef..4a8d8a3c 100644 --- a/src/Backend/EdgeTPU/EdgeTPUToolchain.ts +++ b/src/Backend/EdgeTPU/EdgeTPUToolchain.ts @@ -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"; @@ -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 }; diff --git a/src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts b/src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts index 99ab9fda..e6fc0f64 100644 --- a/src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts +++ b/src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts @@ -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 @@ -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); @@ -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; + 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; - 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() }); }); });