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

Add Kernel SparseReshape for CPU and WebGL backend #4956

Merged
merged 15 commits into from Apr 20, 2021
Merged

Add Kernel SparseReshape for CPU and WebGL backend #4956

merged 15 commits into from Apr 20, 2021

Conversation

pyu10055
Copy link
Collaborator

@pyu10055 pyu10055 commented Apr 19, 2021

ref #4838
TensorFlow python version is defined here
c++ kernel definition is here
To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.


This change is Reviewable

@pyu10055 pyu10055 requested a review from lina128 April 19, 2021 22:39
@google-cla google-cla bot added the cla: yes label Apr 19, 2021
Copy link
Collaborator

@lina128 lina128 left a comment

Choose a reason for hiding this comment

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

Hi Ping, I left some minor comments, overall LGTM. Will converter code be added in a separate PR?

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @pyu10055)


tfjs-backend-cpu/src/kernels/SparseReshape.ts, line 29 at r1 (raw file):

inputIndices.shape.length <= 1

The equivalent of isMatrix is if(inputIndices.shape.length === 2)


tfjs-backend-cpu/src/kernels/SparseReshape.ts, line 50 at r1 (raw file):

      Array.from(backend.data.get(newShape.dataId).values as TypedArray);

  const [newValues, indicesShape, outputShape] = sparseReshapeImpl(

nit: rename to newIndices


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 57 at r1 (raw file):

          'specified input sizes are non-zero');
    }
    const missing = Math.round(denseSize / product);

Should it be Math.trunc?


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 90 at r1 (raw file):

  }

  const newValues =

nit: rename to newIndices


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 92 at r1 (raw file):

  const newValues =
      util.getArrayFromDType(inputDType, nnz * outputRank) as TypedArray;
  util.getArrayFromDType(inputDType, nnz * outputRank);

Remove this line.


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 96 at r1 (raw file):

i * inputRank + j

Is it more readable to write i * inputIndicesStrides[0] + j, where const inputIndicesStrides = util.computeStrides(inputIndicesShape);
Alternatively, add annotation that inputIndices is a matrix, which has shape [numOfIndices, rank].


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 99 at r1 (raw file):

Math.floor(id / outputStrides[j]);

nit: Math.trunc


tfjs-backend-webgl/src/kernels/SparseReshape.ts, line 28 at r1 (raw file):

  const {inputs, backend} = args;
  const {inputIndices, inputShape, newShape} = inputs;
  if (inputIndices.shape.length <= 1) {

!== 2


tfjs-backend-webgl/src/kernels/SparseReshape.ts, line 48 at r1 (raw file):

newValues

nit: rename to newIndices


tfjs-core/src/kernel_names.ts, line 932 at r1 (raw file):

}

export const SparseReshape = 'SparseReshape';

Try to order alphabetically, add this definition after SpaceToBatchND.


tfjs-core/src/kernel_names.ts, line 934 at r1 (raw file):

Quoted 5 lines of code…
export interface SparseReshapeInputs extends NamedTensorInfoMap {
  inputIndices: TensorInfo;
  inputShape: TensorInfo;
  newShape: TensorInfo;
}

Why not just export type SparseReshapeInputs = Pick<NamedTensorInfoMap, 'inputIndices'|'inputShape'|'newShape'>;


tfjs-core/src/ops/ops.ts, line 228 at r1 (raw file):

import * as fused from './fused_ops';
import * as sparse from './sparse_ops';

Can we follow the Image Ops import and export below? Because we don't want to export everything from ./sparse_ops, e.g. impl shouldn't be exported.


tfjs-core/src/ops/sparse_ops.ts, line 18 at r1 (raw file):

 */

import {sparseReshape} from './sparse/sparse_reshape';

This feels redundant, should we just manage all the import export in ops.ts?

Copy link
Collaborator Author

@pyu10055 pyu10055 left a comment

Choose a reason for hiding this comment

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

Thank you, will add converter changes in a separate PR.

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @lina128)


tfjs-backend-cpu/src/kernels/SparseReshape.ts, line 29 at r1 (raw file):

Previously, lina128 (Na Li) wrote…
inputIndices.shape.length <= 1

The equivalent of isMatrix is if(inputIndices.shape.length === 2)

Done.


tfjs-backend-cpu/src/kernels/SparseReshape.ts, line 50 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

nit: rename to newIndices

done


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 57 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

Should it be Math.trunc?

Done.


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 92 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

Remove this line.

Done.


tfjs-backend-cpu/src/kernels/SparseReshape_impl.ts, line 96 at r1 (raw file):

Previously, lina128 (Na Li) wrote…
i * inputRank + j

Is it more readable to write i * inputIndicesStrides[0] + j, where const inputIndicesStrides = util.computeStrides(inputIndicesShape);
Alternatively, add annotation that inputIndices is a matrix, which has shape [numOfIndices, rank].

thanks, added comments.


tfjs-backend-webgl/src/kernels/SparseReshape.ts, line 28 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

!== 2

Done.


tfjs-core/src/kernel_names.ts, line 932 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

Try to order alphabetically, add this definition after SpaceToBatchND.

Done.


tfjs-core/src/kernel_names.ts, line 934 at r1 (raw file):

Previously, lina128 (Na Li) wrote…
export interface SparseReshapeInputs extends NamedTensorInfoMap {
  inputIndices: TensorInfo;
  inputShape: TensorInfo;
  newShape: TensorInfo;
}

Why not just export type SparseReshapeInputs = Pick<NamedTensorInfoMap, 'inputIndices'|'inputShape'|'newShape'>;

Done.


tfjs-core/src/ops/ops.ts, line 228 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

Can we follow the Image Ops import and export below? Because we don't want to export everything from ./sparse_ops, e.g. impl shouldn't be exported.

Done.


tfjs-core/src/ops/sparse_ops.ts, line 18 at r1 (raw file):

Previously, lina128 (Na Li) wrote…

This feels redundant, should we just manage all the import export in ops.ts?

Done.

@pyu10055 pyu10055 merged commit ffb4f24 into master Apr 20, 2021
@pyu10055 pyu10055 deleted the sparse branch April 20, 2021 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants