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

[core] Fix toPixels memLeak for TensorLike inputs. #3073

Merged
merged 9 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
14 changes: 14 additions & 0 deletions tfjs-core/src/ops/array_ops_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,20 @@ describeWithFlags('toPixels no canvas', ALL_ENVS, () => {
const expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]);
expect(data).toEqual(expected);
});

it('does not leak memory', async () => {
const x = tf.tensor2d([[.1], [.2]], [2, 1]);
const startNumTensors = tf.memory().numTensors;
await tf.browser.toPixels(x);
expect(tf.memory().numTensors).toEqual(startNumTensors);
});

it('does not leak memory given a tensor-like object', async () => {
const x = [[10], [20]]; // 2x1;
const startNumTensors = tf.memory().numTensors;
await tf.browser.toPixels(x);
expect(tf.memory().numTensors).toEqual(startNumTensors);
});
});

describeWithFlags('clone', ALL_ENVS, () => {
Expand Down
4 changes: 3 additions & 1 deletion tfjs-core/src/ops/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ export async function toPixels(
let $img = convertToTensor(img, 'img', 'toPixels');
if (!(img instanceof Tensor)) {
// Assume int32 if user passed a native array.
$img = $img.toInt();
const originalImgTensor = $img;
$img = originalImgTensor.toInt();
originalImgTensor.dispose();
}
if ($img.rank !== 2 && $img.rank !== 3) {
throw new Error(
Expand Down