Skip to content

Commit

Permalink
[EdgeTPU] Expand createCfg method (#1740)
Browse files Browse the repository at this point in the history
- Extract and implement function about selecting cfg options with `quick pick`
- Extracting functions to make them easier to scale later

ONE-vscode-DCO-1.0-Signed-off-by: Hyeon-Uk <[email protected]>
  • Loading branch information
dayo09 authored Nov 3, 2023
1 parent 8e23f02 commit d31235b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 23 deletions.
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 @@ -1137,8 +1139,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 @@ -1149,18 +1204,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 @@ -1170,10 +1226,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 @@ -1187,7 +1246,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 @@ -1196,7 +1255,7 @@ input_path=${modelName}.${extName}
vscode.commands.executeCommand(
"vscode.openWith",
uri,
CfgEditorPanel.viewType
cfgInfo.viewType
);
});
} else {
Expand Down

0 comments on commit d31235b

Please sign in to comment.