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

[EdgeTPU] Add EdgeTPUDebianToolchain #1733

Merged
merged 1 commit into from
Nov 2, 2023
Merged
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
80 changes: 80 additions & 0 deletions src/Backend/EdgeTPU/EdgeTPUToolchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Command } from "../Command";
import { DebianToolchain } from "../ToolchainImpl/DebianToolchain";

import * as ini from "ini";
import * as fs from "fs";
import * as path from "path";
import { Logger } from "../../Utils/Logger";

class EdgeTPUDebianToolchain extends DebianToolchain {
run(cfg: string): Command {
let cmd = new Command("edgetpu_compiler");
var config = ini.parse(fs.readFileSync(cfg, "utf-8").trim());

if (config["edgetpu-compile"] === undefined) {
Logger.error(
"EdgeTPUDebianToolchain",
`The configuration file doesn't include 'edgetpu-compile' section.`
);

throw new Error(
`The configuration file doesn't include ''edgetpu-compile' section.`
);
}

let outDir = path.dirname(config["edgetpu-compile"]["output_path"]);
cmd.push("--out_dir");
cmd.push(outDir);

let intermediateTensors = config["edgetpu-compile"]["intermediate_tensors"];
if (intermediateTensors !== undefined) {
cmd.push("--intermediate_tensors");
cmd.push(intermediateTensors);
}

let showOperations = config["edgetpu-compile"]["show_operations"];
if (showOperations === "True") {
cmd.push("--show_operations");
}

let minRuntimeVersion = config["edgetpu-compile"]["min_runtime_version"];
if (minRuntimeVersion !== undefined) {
cmd.push("--min_runtime_version");
cmd.push(minRuntimeVersion);
}

let searchDelegate = config["edgetpu-compile"]["search_delegate"];
if (searchDelegate === "True") {
cmd.push("--search_delegate");
}

let delegateSearchStep = config["edgetpu-compile"]["delegate_search_step"];
if (delegateSearchStep !== undefined) {
cmd.push("--delegate_search_step");
cmd.push(delegateSearchStep);
}

let inputPath = config["edgetpu-compile"]["input_path"];
cmd.push(inputPath);

return cmd;
}
}

export { EdgeTPUDebianToolchain };
119 changes: 119 additions & 0 deletions src/Tests/Backend/EdgeTPU/EdgeTPUToolchain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { assert } from "chai";

import { EdgeTPUDebianToolchain } from "../../../Backend/EdgeTPU/EdgeTPUToolchain";
import { ToolchainInfo } from "../../../Backend/Toolchain";
import { Version } from "../../../Backend/Version";
import { TestBuilder } from "../../TestBuilder";

const content = `
[edgetpu-compile]
input_path=/home/workspace/models/sample.tflite
output_path=/home/workspace/models/sample_edge_tpu.tflite
intermediate_tensors=tensorName1,tensorName2
show_operations=True
min_runtime_version=14
search_delegate=True
delegate_search_step=4
`;

const relativeOutputPathcontent = `
[edgetpu-compile]
input_path=./sample.tflite
output_path=./sample_edge_tpu.tflite
intermediate_tensors=tensorName1,tensorName2
show_operations=True
min_runtime_version=14
search_delegate=True
delegate_search_step=4
`;

suite("Backend", function () {
suite("EdgeTPUDebianToolchain", function () {
let testBuilder: TestBuilder;

setup(() => {
testBuilder = new TestBuilder(this);
testBuilder.setUp();
});

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

suite("#run", function () {
test("returns Command with cfg", function () {
testBuilder.writeFileSync("file.cfg", content);
const cfgFilePath = testBuilder.getPath("file.cfg");

const name = "EdgeTPU";
const desc = "EdgeTPU Compiler";
const version = new Version(0, 1, 0);
const info = new ToolchainInfo(name, desc, version);
let dt = new EdgeTPUDebianToolchain(info);
let cmd = dt.run(cfgFilePath);

const expectedStrs: string[] = [
"edgetpu_compiler",
"--out_dir",
"/home/workspace/models",
"--intermediate_tensors",
"tensorName1,tensorName2",
"--show_operations",
"--min_runtime_version",
"14",
"--search_delegate",
"--delegate_search_step",
"4",
"/home/workspace/models/sample.tflite",
];

assert.deepEqual(cmd, expectedStrs);
});

test("returns Command with cfg containing relative input path", function () {
testBuilder.writeFileSync("file.cfg", relativeOutputPathcontent);
const cfgFilePath = testBuilder.getPath("file.cfg");

const name = "EdgeTPU";
const desc = "EdgeTPU Compiler";
const version = new Version(0, 1, 0);
const info = new ToolchainInfo(name, desc, version);
let dt = new EdgeTPUDebianToolchain(info);
let cmd = dt.run(cfgFilePath);

const expectedStrs: string[] = [
"edgetpu_compiler",
"--out_dir",
".",
"--intermediate_tensors",
"tensorName1,tensorName2",
"--show_operations",
"--min_runtime_version",
"14",
"--search_delegate",
"--delegate_search_step",
"4",
"./sample.tflite",
];

assert.deepEqual(cmd, expectedStrs);
});
});
});
});