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

Completed constant #65

Merged
merged 5 commits into from
Jan 11, 2024
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
22 changes: 22 additions & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

import {cast} from './cast.js';
import {Tensor, sizeOfShape} from '../src/lib/tensor.js';
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this function's description and also add descriptions for start, step and type parameters.

* Create a constant array of specified data type and shape,
* which contains data incrementing by step.
* @param {Number} start
* @param {Number} step
* @param {Array} outputShape
* @param {string} type
* @return {Tensor}
*/
export function constant(start, step, outputShape, type = 'float32') {
const outputElementCount = sizeOfShape(outputShape);
const data = [];
for (let i = 0; i < outputElementCount; i++) {
data.push(start + i * step);
}
const tensor = new Tensor(outputShape, data);
return cast(tensor, type);
}
60 changes: 60 additions & 0 deletions test/constant_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

import {constant} from '../src/constant.js';
import * as utils from './utils.js';


describe('test constant', function() {
function testConstant(start, step, outputShape, type, expected) {
const outputTensor = constant(start, step, outputShape, type);
Copy link

@fdwr fdwr Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be some fallout from the discussion of webmachinelearning/webnn#492, but it would be a minor incremental change. e.g.

const outputTensor = constant({dimensions: outputShape, dataType: dataType}, start, step);

(so, I don't see a reason to block either way?)

utils.checkShape(outputTensor, expected.shape);
utils.checkValue(outputTensor, expected.data);
}

it('constant step > 0', function() {
const expected ={
shape: [3, 3],
data: [
0, 1, 2,
3, 4, 5,
6, 7, 8,
],
};
testConstant(
0, 1, [3, 3], 'float32', expected);
});

it('constant step < 0', function() {
const expected ={
shape: [3, 3],
data: [
9, 8, 7,
6, 5, 4,
3, 2, 1,
],
};
testConstant(
9, -1, [3, 3], 'float32', expected);
});

it('constant step = 0', function() {
const expected ={
shape: [3, 3],
data: [
1, 1, 1,
1, 1, 1,
1, 1, 1,
],
};
testConstant(
1, 0, [3, 3], 'float64', expected);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to include one scalar sanity-check case. e.g.

  it('constant scalar', function() {
    const expected ={
      shape: [],
      data: [42],
    };
    testConstant(42, 0, [], 'float64', expected);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


it('constant scalar', function() {
const expected ={
shape: [],
data: [42],
};
testConstant(42, 0, [], 'float64', expected);
});
});