Skip to content

Commit

Permalink
[OV JS] Add TS definition for AnyMap (#26711)
Browse files Browse the repository at this point in the history
### Details:
- Add definition of `OVAny` to `addon.ts` to encapsulate the concept of
[ov::Any](https://docs.openvino.ai/2024/api/c_cpp_api/classov_1_1_any.html)
from C++ API in a way that's clear and can be easily updated if the
underlying C++ type changes. Improved readability and maintainability.
-- 'OVAny' corresponds to Python API
[OVAny](https://docs.openvino.ai/2024/api/ie_python_api/_autosummary/openvino.OVAny.html)
- In future updates to Node.js API, the OVAny type is expected to be
enhanced with additional functionality.
 - Missed linter changes to `core.test.js` due to upstream merges

### Tickets:
 - [CVS-149319](https://jira.devtools.intel.com/browse/CVS-149319)
  • Loading branch information
almilosz authored Oct 2, 2024
1 parent 98adad1 commit 32aaa2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
35 changes: 16 additions & 19 deletions src/bindings/js/node/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type elementTypeString =
| 'f32'
| 'string';

type OVAny = string | number | boolean;

/**
* Core represents an OpenVINO runtime Core entity.
*
Expand Down Expand Up @@ -48,7 +50,7 @@ interface Core {
compileModel(
model: Model,
deviceName: string,
config?: { [propertyName: string]: string },
config?: Record<string, OVAny>,
): Promise<CompiledModel>;
/**
* Asynchronously reads a model and creates a compiled model
Expand All @@ -67,7 +69,7 @@ interface Core {
compileModel(
modelPath: string,
deviceName: string,
config?: { [propertyName: string]: string },
config?: Record<string, OVAny>,
): Promise<CompiledModel>;
/**
* A synchronous version of {@link Core.compileModel}.
Expand All @@ -76,7 +78,7 @@ interface Core {
compileModelSync(
model: Model,
deviceName: string,
config?: { [propertyName: string]: string },
config?: Record<string, OVAny>,
): CompiledModel;
/**
* A synchronous version of {@link Core.compileModel}.
Expand All @@ -85,7 +87,7 @@ interface Core {
compileModelSync(
modelPath: string,
deviceName: string,
config?: { [propertyName: string]: string },
config?: Record<string, OVAny>,
): CompiledModel;
/**
* It returns a list of available inference devices.
Expand All @@ -101,7 +103,7 @@ interface Core {
* It gets the properties dedicated to device behaviour.
* @param propertyName A property name.
*/
getProperty(propertyName: string): string | number | boolean;
getProperty(propertyName: string): OVAny;

/**
* It gets the properties dedicated to device behaviour.
Expand All @@ -111,7 +113,7 @@ interface Core {
getProperty(
deviceName: string,
propertyName: string,
): string | number | boolean;
): OVAny;
/**
* It returns information on the version of device plugins.
* @param deviceName A device name to identify a plugin.
Expand All @@ -135,7 +137,7 @@ interface Core {
importModel(
modelStream: Buffer,
device: string,
config?: { [key: string]: string | number | boolean },
config?: Record<string, OVAny>,
): Promise<CompiledModel>;
/**
* A synchronous version of {@link Core.importModel}.
Expand All @@ -144,7 +146,7 @@ interface Core {
importModelSync(
modelStream: Buffer,
device: string,
config?: { [key: string]: string | number | boolean },
config?: Record<string, OVAny>,
): CompiledModel;
/**
* It reads models from the IR / ONNX / PDPD / TF and TFLite formats.
Expand Down Expand Up @@ -197,16 +199,13 @@ interface Core {
* It sets the properties.
* @param properties An object with the property name - property value pairs.
*/
setProperty(properties: { [key: string]: string | number | boolean }): void;
setProperty(properties: Record<string, OVAny>): void;
/**
* It sets the properties for a device.
* @param deviceName The name of a device.
* @param properties An object with the property name - property value pairs.
*/
setProperty(
deviceName: string,
properties: { [key: string]: string | number | boolean },
): void;
setProperty(deviceName: string, properties: Record<string, OVAny>): void;
/**
* It queries the device if it supports specified model with the specified
* properties.
Expand All @@ -218,8 +217,8 @@ interface Core {
queryModel(
model: Model,
deviceName: string,
properties?: {[key: string]: string | number | boolean},
): {[key: string]: string | number | boolean};
properties?: Record<string, OVAny>,
): { [key: string]: string };
}
interface CoreConstructor {
new (): Core;
Expand Down Expand Up @@ -325,7 +324,7 @@ interface CompiledModel {
* @param propertyName A string to get the property value.
* @returns The property value.
*/
getProperty(propertyName: string): string | number | boolean;
getProperty(propertyName: string): OVAny;
/**
* It creates an inference request object used to infer the compiled model.
* @return {InferRequest}
Expand Down Expand Up @@ -380,9 +379,7 @@ interface CompiledModel {
* @param property An object with the key-value pairs.
* (property name, property value)
*/
setProperty(properties: {
[propertyName: string]: string | number | boolean;
}): void;
setProperty(properties: Record<string, OVAny>): void;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/bindings/js/node/tests/unit/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('ov.Core tests', () => {
before(async () => {
await isModelAvailable(testModels.testModelFP32);
});

beforeEach(() => {
core = new ov.Core();
});

it('Core.setProperty()', () => {
const tmpDir = '/tmp';

Expand Down Expand Up @@ -83,29 +83,29 @@ describe('ov.Core tests', () => {
it('Core.queryModel() with empty parameters should throw an error', () => {
assert.throws(
() => core.queryModel().then(),
/'queryModel' method called with incorrect parameters./
)
/'queryModel' method called with incorrect parameters./,
);
});

it('Core.queryModel() with less arguments should throw an error', () => {
assert.throws(
() => core.queryModel("Unexpected Argument").then(),
/'queryModel' method called with incorrect parameters./
)
() => core.queryModel('Unexpected Argument').then(),
/'queryModel' method called with incorrect parameters./,
);
});

it('Core.queryModel() with incorrect arguments should throw an error', () => {
const model = core.readModelSync(getModelPath().xml);
assert.throws(
() => core.queryModel(model, "arg1", "arg2").then(),
/'queryModel' method called with incorrect parameters./
)
() => core.queryModel(model, 'arg1', 'arg2').then(),
/'queryModel' method called with incorrect parameters./,
);
});

it('Core.queryModel() should have device in the result values', () => {
const model = core.readModelSync(getModelPath().xml);
const device = 'CPU';
const query_model = core.queryModel(model, device);
assert(Object.values(query_model).includes(device));
const queryModel = core.queryModel(model, device);
assert(Object.values(queryModel).includes(device));
});
});

0 comments on commit 32aaa2f

Please sign in to comment.