Skip to content

Commit dc699e9

Browse files
committed
[add] tensorset now supports tensors created from buffer data
1 parent 845b791 commit dc699e9

File tree

9 files changed

+700
-156
lines changed

9 files changed

+700
-156
lines changed

package-lock.json

Lines changed: 593 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@
4040
"@types/redis": "^2.8.21"
4141
},
4242
"devDependencies": {
43+
"@babel/polyfill": "^7.8.7",
4344
"@istanbuljs/nyc-config-typescript": "^1.0.1",
4445
"@types/chai": "^4.2.11",
4546
"@types/mocha": "^7.0.2",
4647
"@types/node": "^13.13.4",
4748
"@types/redis": "^2.8.21",
4849
"chai": "^4.2.0",
4950
"codecov": "^3.6.5",
51+
"core-js": "^2.5.7",
5052
"coveralls": "^3.1.0",
53+
"jimp": "0.8.4",
5154
"mocha": "^7.1.2",
5255
"np": "^6.2.3",
5356
"nyc": "^15.0.1",

src/client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Callback, RedisClient } from 'redis';
1+
import { RedisClient } from 'redis';
22
import { Tensor } from './tensor';
33
import { Model } from './model';
44
import * as util from 'util';
@@ -29,8 +29,13 @@ export class Client {
2929
const args: any[] = [keName, t.dtype];
3030
t.shape.forEach((value) => args.push(value.toString()));
3131
if (t.data != null) {
32-
args.push('VALUES');
33-
t.data.forEach((value) => args.push(value.toString()));
32+
if (t.data instanceof Buffer) {
33+
args.push('BLOB');
34+
args.push(t.data);
35+
} else {
36+
args.push('VALUES');
37+
t.data.forEach((value) => args.push(value.toString()));
38+
}
3439
}
3540
return this._sendCommand('ai.tensorset', args);
3641
}

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Helpers {
2121
* TODO: document
2222
* @param buffer
2323
*/
24-
normalizeRGB(buffer: Float32Array): Float32Array {
24+
normalizeRGB(buffer: any): Float32Array {
2525
const npixels = buffer.length / 4;
2626
const out = new Float32Array(npixels * 3);
2727

src/stats.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,6 @@
44
import { Backend } from './backend';
55

66
export class Stats {
7-
get backend(): Backend {
8-
return this._backend;
9-
}
10-
11-
set backend(value: Backend) {
12-
this._backend = value;
13-
}
14-
get type(): string {
15-
return this._type;
16-
}
17-
18-
set type(value: string) {
19-
this._type = value;
20-
}
21-
get key(): string {
22-
return this._key;
23-
}
24-
25-
set key(value: string) {
26-
this._key = value;
27-
}
28-
get errors(): number {
29-
return this._errors;
30-
}
31-
32-
set errors(value: number) {
33-
this._errors = value;
34-
}
35-
get calls(): number {
36-
return this._calls;
37-
}
38-
39-
set calls(value: number) {
40-
this._calls = value;
41-
}
42-
get samples(): number {
43-
return this._samples;
44-
}
45-
46-
set samples(value: number) {
47-
this._samples = value;
48-
}
49-
get duration(): number {
50-
return this._duration;
51-
}
52-
53-
set duration(value: number) {
54-
this._duration = value;
55-
}
567
/**
578
*
589
* @param key - a String of the name of the key storing the model or script value
@@ -86,19 +37,77 @@ export class Stats {
8637
// the cumulative duration of executions in microseconds
8738
private _duration: number;
8839

40+
get duration(): number {
41+
return this._duration;
42+
}
43+
44+
set duration(value: number) {
45+
this._duration = value;
46+
}
47+
8948
// the cumulative number of samples obtained from the 0th (batch) dimension (only applicable for RedisAI models)
9049
private _samples: number;
9150

51+
get samples(): number {
52+
return this._samples;
53+
}
54+
55+
set samples(value: number) {
56+
this._samples = value;
57+
}
58+
9259
// the total number of executions
9360
private _calls: number;
9461

62+
get calls(): number {
63+
return this._calls;
64+
}
65+
66+
set calls(value: number) {
67+
this._calls = value;
68+
}
69+
9570
// the total number of errors generated by executions (excluding any errors generated during parsing commands)
9671
private _errors: number;
9772

73+
get errors(): number {
74+
return this._errors;
75+
}
76+
77+
set errors(value: number) {
78+
this._errors = value;
79+
}
80+
9881
private _key: string;
82+
83+
get key(): string {
84+
return this._key;
85+
}
86+
87+
set key(value: string) {
88+
this._key = value;
89+
}
90+
9991
private _type: string;
92+
93+
get type(): string {
94+
return this._type;
95+
}
96+
97+
set type(value: string) {
98+
this._type = value;
99+
}
100+
100101
private _backend: Backend;
101102

103+
get backend(): Backend {
104+
return this._backend;
105+
}
106+
107+
set backend(value: Backend) {
108+
this._backend = value;
109+
}
110+
102111
// tag is an optional string for tagging the model/script such as a version number or any arbitrary identifier
103112
private _tag: string | undefined;
104113

src/tensor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class Tensor {
1010
* @param shape one or more dimensions, or the number of elements per axis, for the tensor
1111
* @param data numeric data provided by one or more subsequent val arguments
1212
*/
13-
constructor(dtype: Dtype, shape: number[], data: number[] | null) {
13+
constructor(dtype: Dtype, shape: number[], data: Buffer | number[] | null) {
1414
this._shape = shape;
1515
this._dtype = dtype;
1616
if (data != null) {
@@ -38,13 +38,13 @@ export class Tensor {
3838
this._shape = value;
3939
}
4040

41-
private _data: number[] | null;
41+
private _data: Buffer | number[];
4242

43-
get data(): number[] | null {
43+
get data(): Buffer | number[] | null {
4444
return this._data;
4545
}
4646

47-
set data(value: number[] | null) {
47+
set data(value: Buffer | number[] | null) {
4848
this._data = value;
4949
}
5050
}

tests/test_client.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Model } from '../src/model';
88
import * as fs from 'fs';
99
import { Backend } from '../src/backend';
1010
import { Script } from '../src/script';
11+
import { Helpers } from '../src';
12+
// tslint:disable-next-line:no-var-requires
13+
const Jimp = require('jimp');
1114

1215
const mochaAsync = (fn: any) => {
1316
return (done: any) => {
@@ -88,6 +91,25 @@ it(
8891
}),
8992
);
9093

94+
it(
95+
'ai.tensorset/ai.tensorget positive testing with default data',
96+
mochaAsync(async () => {
97+
const nativeClient = createClient();
98+
const aiclient = new Client(nativeClient);
99+
const inputImage = await Jimp.read('./tests/test_data/panda-224x224.jpg');
100+
const imageWidth = 224;
101+
const imageHeight = 224;
102+
const image = inputImage.cover(imageWidth, imageHeight);
103+
const helpers = new Helpers();
104+
const normalized = helpers.normalizeRGB(image.bitmap.data);
105+
const buffer = Buffer.from(normalized.buffer);
106+
const tensor = new Tensor(Dtype.float32, [imageWidth, imageHeight, 3], buffer);
107+
const result = await aiclient.tensorset('tensor-image', tensor);
108+
expect(result).to.equal('OK');
109+
aiclient.end(true);
110+
}),
111+
);
112+
91113
it(
92114
'ai.tensorget negative testing',
93115
mochaAsync(async () => {

tsconfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"lib": [
1212
"es2015"
1313
],
14-
"include": ["src"],
15-
"exclude": ["node_modules", "**/__tests__/*"]
14+
"include": [
15+
"src"
16+
],
17+
"exclude": [
18+
"node_modules",
19+
"**/__tests__/*"
20+
]
1621
}

tslint.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"defaultSeverity": "error",
3-
"extends": ["tslint:recommended","tslint-config-prettier"],
3+
"extends": [
4+
"tslint:recommended",
5+
"tslint-config-prettier"
6+
],
47
"jsRules": {},
58
"rules": {
69
"no-console": false

0 commit comments

Comments
 (0)