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] Introduce EdgeTpuConfigSetting #1735

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
26 changes: 23 additions & 3 deletions src/OneExplorer/ConfigObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ import { RealPath } from "../Utils/Helpers";
import { Logger } from "../Utils/Logger";

import { Artifact } from "./ArtifactLocator";
import { OneCfg, OneConfigSetting } from "./ConfigSettings/OneConfigSetting";
import { ConfigSetting } from "./ConfigSetting";
import { OneCfg, OneConfigSetting } from "./ConfigSettings/OneConfigSetting";
import {
EdgeTpuCfg,
EdgeTpuConfigSetting,
} from "./ConfigSettings/EdgeTpuConfigSetting";

/**
* Type for rawObj.
* Expand for additional Backend's section value
*/
type Cfg = OneCfg;
type Cfg = OneCfg & EdgeTpuCfg;
type CfgKeys = keyof Cfg;

/**
* Enum for what type of ConfigSetting that ConfigObject use
*/
enum CfgType {
one,
edgeTpu,
}

/**
Expand Down Expand Up @@ -127,6 +132,9 @@ export class ConfigObj {
public get configSetting(): ConfigSetting {
let configSetting: ConfigSetting;
switch (this.configType) {
case CfgType.edgeTpu:
configSetting = new EdgeTpuConfigSetting();
break;
case CfgType.one:
default:
configSetting = new OneConfigSetting();
Expand All @@ -139,7 +147,15 @@ export class ConfigObj {
private constructor(uri: vscode.Uri, rawObj: Cfg) {
this.uri = uri;
this.rawObj = rawObj;
this.configType = CfgType.one;
const ext = path.extname(uri.fsPath);
switch (ext) {
case EdgeTpuConfigSetting.ext:
this.configType = CfgType.edgeTpu;
break;
case OneConfigSetting.ext:
default:
this.configType = CfgType.one;
}

// TODO: separate to init()
const configSetting = this.configSetting;
Expand Down Expand Up @@ -212,6 +228,10 @@ export class ConfigObj {
);
}

// EdgeTpu Compiler's output_path is fixed
// ex) `input_path`_edgetpu.tflite
this.configSetting.updateOutPath(newpath, this.rawObj, kSection);

return vscode.workspace.fs.writeFile(
this.uri,
new TextEncoder().encode(ini.stringify(this.rawObj))
Expand Down
126 changes: 126 additions & 0 deletions src/OneExplorer/ConfigSettings/EdgeTpuConfigSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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 * as vscode from "vscode";
import * as path from "path";

import { Locator, LocatorRunner } from "../ArtifactLocator";
import { ConfigSetting } from "../ConfigSetting";

export type EdgeTpuCfg = {
"edgetpu-compile": any;
};

export class EdgeTpuConfigSetting extends ConfigSetting {
static backendName = "EdgeTPU";
static ext = ".edgetpucfg";

constructor() {
super();
this.sections = {
".tflite": "edgetpu-compile",
};
}

/**
* Add postfix(_edgetpu) to file name of new input path
* @returns void
*/
public updateOutPath(
newpath: string,
rawObj: { [key: string]: any },
kSection: string
): void {
const ext = path.extname(newpath);
const name = path.basename(newpath, ext) + "_edgetpu" + ext;
const dir = path.dirname(newpath);
const outpath = path.join(dir, name);
if (rawObj[kSection]) {
rawObj[kSection].output_path = outpath;
}
}

protected _initBaseModelsLocatorRunner() {
let locatorRunner = new LocatorRunner();

locatorRunner.register({
artifactAttr: {
ext: ".tflite",
icon: new vscode.ThemeIcon("symbol-variable"),
},
locator: new Locator((value: string) => {
value += "";
const filterd = value
.split(" ")
.filter((val) => !val.endsWith("_edgetpu.tflite"));
value = filterd.join(" ");
return LocatorRunner.searchWithExt(".tflite", value);
}),
});

this.baseModelsLocatorRunner = locatorRunner;
}

protected _initProductsLocatorRunner() {
let locatorRunner = new LocatorRunner();

/**
* ABOUT ORDERING
*
* The registration order determines the order in the tree view
*/

// NOTE
// Shows <model>_edgetpu.tflite
// <model>_edgetpu.tflite generated by <model>.tflite is product type
locatorRunner.register({
artifactAttr: {
ext: ".tflite",
icon: new vscode.ThemeIcon("symbol-variable"),
},
locator: new Locator((value: string) => {
value += "";
const filterd = value
.split(" ")
.filter((val) => val.endsWith("_edgetpu.tflite"));
value = filterd.join(" ");
return LocatorRunner.searchWithExt(".tflite", value);
}),
});

locatorRunner.register({
// 'default' view type is 'text editor' (vscode.openWith)
artifactAttr: {
ext: ".log",
openViewType: "default",
icon: vscode.ThemeIcon.File,
canHide: true,
},
locator: new Locator((value: string) => {
value += "";
const filterd = value
.split(" ")
.filter((val) => val.endsWith("_edgetpu.tflite"));
value = filterd.join(" ");
return LocatorRunner.searchWithExt(".tflite", value).map((filepath) =>
filepath.replace(".tflite", ".log")
);
}),
});

this.productsLocatorRunner = locatorRunner;
}
}
26 changes: 26 additions & 0 deletions src/Tests/OneExplorer/ConfigObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ConfigObj } from "../../OneExplorer/ConfigObject";

import { TestBuilder } from "../TestBuilder";
import { OneConfigSetting } from "../../OneExplorer/ConfigSettings/OneConfigSetting";
import { EdgeTpuConfigSetting } from "../../OneExplorer/ConfigSettings/EdgeTpuConfigSetting";

suite("OneExplorer", function () {
suite("ConfigObject", function () {
Expand Down Expand Up @@ -101,6 +102,31 @@ suite("OneExplorer", function () {
assert.isTrue(configObj!.configSetting instanceof OneConfigSetting);
}
});

test("Get edge tpu config setting with .edgetpucfg file", function () {
const configName = "model.edgetpucfg";

const content = `
`;

// Write a file inside temp directory
testBuilder.writeFileSync(configName, content);

// Get file paths inside the temp directory
const configPath = testBuilder.getPath(configName);

const configObj = ConfigObj.createConfigObj(
vscode.Uri.file(configPath)
);

// Validate
{
assert.isDefined(configObj);
assert.isTrue(
configObj!.configSetting instanceof EdgeTpuConfigSetting
);
}
});
});

suite("#one-import-onnx section", function () {
Expand Down
74 changes: 74 additions & 0 deletions src/Tests/OneExplorer/ConfigSettings/EdgeTpuConfigSetting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 * as ini from "ini";

import { LocatorRunner } from "../../../OneExplorer/ArtifactLocator";
import { EdgeTpuConfigSetting } from "../../../OneExplorer/ConfigSettings/EdgeTpuConfigSetting";

suite("OneExplorer", function () {
suite("ConfigSettings", function () {
suite("EdgeTpuConfigSetting", function () {
suite("#constructor", function () {
test("Create one config setting", function () {
assert.doesNotThrow(() => new EdgeTpuConfigSetting());

const configSetting = new EdgeTpuConfigSetting();
assert.isTrue(configSetting instanceof EdgeTpuConfigSetting);
});
});

suite("#init()", function () {
test("init one config setting", function () {
const configSetting = new EdgeTpuConfigSetting();
assert.isTrue(configSetting instanceof EdgeTpuConfigSetting);

assert.doesNotThrow(() => configSetting.init());
assert.isTrue(
configSetting.baseModelsLocatorRunner instanceof LocatorRunner
);
assert.isTrue(
configSetting.productsLocatorRunner instanceof LocatorRunner
);
});
});

suite("#updateOutPath()", function () {
test("edgetpu config setting change output_path by adding postfix(_edgetpu) to new_path", function () {
const inputPath = "model.tflite";
const outputPath = "model_edgetpu.tflite";
const kSection = "edgetpu-compile";
const edgetpucfg = `
[edgetpu-compile]
input_path=${inputPath}
`;
const rawObj = ini.parse(edgetpucfg);

const configSetting = new EdgeTpuConfigSetting();
assert.isTrue(configSetting instanceof EdgeTpuConfigSetting);

assert.doesNotThrow(() =>
configSetting.updateOutPath(inputPath, rawObj, kSection)
);
console.log(rawObj);
assert.isDefined(rawObj["edgetpu-compile"].output_path);
assert.equal(outputPath, rawObj["edgetpu-compile"].output_path);
});
});
});
});
});