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

Conversation

mei1127
Copy link
Contributor

@mei1127 mei1127 commented Dec 19, 2023

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

Choose a reason for hiding this comment

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

Please remove these two console.log codes.

Copy link
Contributor

@BruceDai BruceDai left a comment

Choose a reason for hiding this comment

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

Thanks @mei1127 for implementing this constant.
Regarding to the CI failure, it depends on #63, please update commit to re-trigger CI checking once #63 was landed, thanks.

src/constant.js Outdated
resultArray.push(start + i * step);
}
const resultToTensor = new Tensor([resultArray.length], resultArray);
const transformTensorType = cast(resultToTensor, type);
Copy link

@miaobin miaobin Dec 20, 2023

Choose a reason for hiding this comment

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

Since we did not make corresponding checks or specifications for data out of bounds in the #63 cast function, should we clarify the behavior of the cases here?

  1. constant(start = 127, step = 1, outputShape = [3], type = 'int8');
  2. constant(start = -10, step = 1, outputShape = [3, 3], type = 'uint32');
  3. constant(start = 10, step = -2, outputShape = [3, 3], type = 'uint32');
    ......

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Different hardware will behave differently (CPU, NPU, GPU), so I think these cases may be unnecessary.

Copy link

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

Thanks Mei.

src/constant.js Outdated
for (let i = 0; i < outputElementCount; i++) {
resultArray.push(start + i * step);
}
const resultToTensor = new Tensor([resultArray.length], resultArray);
Copy link

Choose a reason for hiding this comment

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

Just create the tensor with the output shape upfront?

  const resultToTensor = new Tensor(outputShape, resultArray);
  const output = cast(resultToTensor, type);
  return output;

};
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

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.

Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

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

lgtm with nits.

src/constant.js Outdated

export function constant(start, step, outputShape, type = 'float32') {
const outputElementCount = sizeOfShape(outputShape);
const resultArray = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: resultArray -> data which aligns with "data" in your description.

src/constant.js Outdated
for (let i = 0; i < outputElementCount; i++) {
resultArray.push(start + i * step);
}
const resultToTensor = new Tensor(outputShape, resultArray);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: resultToTensor -> tensor

src/constant.js Outdated
}
const resultToTensor = new Tensor(outputShape, resultArray);
const output = cast(resultToTensor, type);
return output;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: merge the above two lines into one return cast(tensor, type);


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?)

Copy link

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

Thanks Mei.

@huningxin
Copy link
Contributor

@BruceDai , please take another look.

@BruceDai
Copy link
Contributor

LGTM, thanks @mei1127 .

@huningxin huningxin merged commit 86338a5 into webmachinelearning:main Jan 11, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants