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] Expand createCfg method #1740

Merged
merged 1 commit into from
Nov 3, 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
17 changes: 15 additions & 2 deletions src/CfgEditor/CfgData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as ini from "ini";
import { ICfgData } from "./ICfgData";
import { CfgInfo, ICfgData } from "./ICfgData";
import { Sections } from "./Sections";

// NOTE: Why is not function overloadding used? Its maintain costs expensive.
Expand All @@ -33,7 +33,7 @@ import { Sections } from "./Sections";
//
export class CfgData extends ICfgData {
constructor(cfg = undefined) {
super(cfg, Sections.onecc);
super(cfg, Sections.onecc, "one.editor.cfg", ".cfg");
}

setWithConfig(cfg: any): void {
Expand Down Expand Up @@ -86,4 +86,17 @@ export class CfgData extends ICfgData {
config[section] = ini.parse(value);
this.resolveDeprecated();
}

generateCfgInfo(modelName: string, extName: string): CfgInfo {
return {
title: `Create ONE configuration of '${modelName}.${extName}' :`,
viewType: this.viewType,
extType: this.extType,
content: `[onecc]
one-import-${extName}=True
[one-import-${extName}]
input_path=${modelName}.${extName}
`,
};
}
}
20 changes: 18 additions & 2 deletions src/CfgEditor/EdgeTPUCfgData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/

import * as ini from "ini";
import { ICfgData } from "./ICfgData";
import { CfgInfo, ICfgData } from "./ICfgData";
import { Sections } from "./Sections";

export class EdgeTPUCfgData extends ICfgData {
constructor(cfg = undefined) {
super(cfg, Sections.edgetpu);
super(cfg, Sections.edgetpu, "one.editor.edgetpucfg", ".edgetpucfg");
}

setWithConfig(cfg: any): void {
Expand Down Expand Up @@ -48,4 +48,20 @@ export class EdgeTPUCfgData extends ICfgData {
const config = this.getAsConfig();
config[section] = ini.parse(value);
}

generateCfgInfo(modelName: string, extName: string): CfgInfo {
return {
title: `Create EdgeTPU configuration of '${modelName}.${extName}' :`,
viewType: this.viewType,
extType: this.extType,
content: `[edgetpu-compiler]
edgetpu-compile=True
edgetpu-profile=False

[edgetpu-compile]
input_path=${modelName}.${extName}
output_path=${modelName}_edgetpu.${extName}
`,
};
}
}
31 changes: 28 additions & 3 deletions src/CfgEditor/ICfgData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@
*/
import * as ini from "ini";

export type CfgInfo = {
title: string;
viewType: string;
extType: string;
content: string;
};

export abstract class ICfgData {
//config can be undefined
private _config: any;
//section must be existed
private _section!: string[];
private _viewType!: string;
private _extType!: string;

constructor(_config: any, _section: string[]) {
constructor(
_config: any,
_section: string[],
_viewType: string,
_extType: string
) {
this._config = _config;
this._section = _section;
this._viewType = _viewType;
this._extType = _extType;
}

// sets data with object decoded or parsed
Expand All @@ -36,6 +50,17 @@ export abstract class ICfgData {
value: string
): void;
abstract updateSectionWithValue(section: string, value: string): void;
//Return information about each cfgType
abstract generateCfgInfo(modelName: string, extName: string): CfgInfo;

//getter
get viewType(): string {
return this._viewType;
}

get extType(): string {
return this._extType;
}

// set cfgData's config
// only child class can use this method
Expand Down
91 changes: 75 additions & 16 deletions src/OneExplorer/OneExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";

import { CfgEditorPanel } from "../CfgEditor/CfgEditorPanel";
import { obtainWorkspaceRoots } from "../Utils/Helpers";
import { Logger } from "../Utils/Logger";

import { ConfigObj } from "./ConfigObject";
import { ArtifactAttr } from "./ArtifactLocator";
import { OneStorage } from "./OneStorage";
import { CfgInfo, ICfgData } from "../CfgEditor/ICfgData";
import { EdgeTPUCfgData } from "../CfgEditor/EdgeTPUCfgData";
import { CfgData } from "../CfgEditor/CfgData";

// Exported for unit testing only
export {
Expand Down Expand Up @@ -1107,8 +1109,61 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
}

/**
* Create ONE configuration file for a base model
* Input box is prefilled as <base model's name>.cfg
* Select the option for the configuration you want to create
* Return information about the selected option
*
* @param modelName A base model's name
* @param extName A base model's extension name
*
*/
private async generateCfgInfo(
modelName: string,
extName: string
): Promise<CfgInfo | undefined> {
//Options must be added according to extension
const options: vscode.QuickPickItem[] = [
{ label: ".cfg", description: "configuration file of onecc compiler" },
];

if (extName === "tflite") {
options.push({
label: ".edgetpucfg",
description: "configuration file of edge tpu compiler",
});
}

const placeHolder = options.map((option) => option.label).join(" / ");

let selectedLabel: string | undefined = ".cfg";
let cfgData: ICfgData | undefined = undefined;

//If options array only has the `.cfg` option, skip selecting it.
if (options.length !== 1) {
const selectedOption = await vscode.window.showQuickPick(options, {
title: "Pick a configuration to create",
placeHolder: placeHolder,
});
selectedLabel = selectedOption?.label;
}

switch (selectedLabel) {
case ".cfg":
cfgData = new CfgData();
break;
case ".edgetpucfg":
cfgData = new EdgeTPUCfgData();
break;
default:
cfgData = undefined;
break;
}

return cfgData?.generateCfgInfo(modelName, extName);
}

/**
* A configuration file is created for the selected option.
* Input box is prefilled as <base model's name>.<selected option>
* The operation will be cancelled if the file already exists.
*
* @command one.explorer.createCfg
Expand All @@ -1119,18 +1174,19 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
const modelName = path.parse(node.path).name;
const extName = path.parse(node.path).ext.slice(1);

// TODO(dayo) Auto-configure more fields
const content = `[onecc]
one-import-${extName}=True
[one-import-${extName}]
input_path=${modelName}.${extName}
`;
const cfgInfo = await this.generateCfgInfo(modelName, extName);

//When the user presses the ESC button, it is cancelled
if (cfgInfo === undefined) {
return;
}

// TODO(dayo) Auto-configure more fields
const validateInputPath = (cfgName: string): string | undefined => {
const cfgPath: string = path.join(dirPath, cfgName);

if (!cfgName.endsWith(".cfg")) {
return `A file extension must be .cfg`;
if (!cfgName.endsWith(cfgInfo.extType)) {
return `A file extension must be ${cfgInfo.extType}`;
}

if (fs.existsSync(cfgPath)) {
Expand All @@ -1140,10 +1196,13 @@ input_path=${modelName}.${extName}

vscode.window
.showInputBox({
title: `Create ONE configuration of '${modelName}.${extName}' :`,
title: cfgInfo.title,
placeHolder: `Enter a file name`,
value: `${modelName}.cfg`,
valueSelection: [0, `${modelName}.cfg`.length - `.cfg`.length],
value: `${modelName}${cfgInfo.extType}`,
valueSelection: [
0,
`${modelName}${cfgInfo.extType}`.length - `${cfgInfo.extType}`.length,
],
validateInput: validateInputPath,
})
.then((value) => {
Expand All @@ -1157,7 +1216,7 @@ input_path=${modelName}.${extName}

const edit = new vscode.WorkspaceEdit();
edit.createFile(uri);
edit.insert(uri, new vscode.Position(0, 0), content);
edit.insert(uri, new vscode.Position(0, 0), cfgInfo.content);

vscode.workspace.applyEdit(edit).then((isSuccess) => {
if (isSuccess) {
Expand All @@ -1166,7 +1225,7 @@ input_path=${modelName}.${extName}
vscode.commands.executeCommand(
"vscode.openWith",
uri,
CfgEditorPanel.viewType
cfgInfo.viewType
);
});
} else {
Expand Down