Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Avoid disposing intermediate tensor in grad #1013

Merged

Conversation

Lewuathe
Copy link
Contributor

@Lewuathe Lewuathe commented May 3, 2018

Purpose

Fix tensorflow/tfjs#227.
As well as other ops, grad should not dispose intermediate result. We should be able to call dispose explicitly or use tidy in such case.


This change is Reviewable

Copy link
Contributor

@bileschi bileschi left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this!

const x = tf.tensor1d([2, 3]);
const data = gg(x).dataSync();

expect(data[0]).toEqual(12);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the same syntax for checking both data[0] and data[1]

@nsthorat
Copy link
Contributor

nsthorat commented May 3, 2018

Review status: 0 of 2 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


src/gradients.ts, line 146 at r1 (raw file):

                'shape returned by f([x1,...])');
      }
      value.dispose();

ah this is going to introduce a memory leak, but your obversation is absolutely correct. Can you do this with a tf.tidy() instead of the dispose?


src/tensor_test.ts, line 1103 at r1 (raw file):

Previously, bileschi (Stanley Bileschi) wrote…

Please use the same syntax for checking both data[0] and data[1]

You should use expectArraysClose. This method can assert a tensor and a vanilla JS array have the same values.


Comments from Reviewable

@nsthorat
Copy link
Contributor

nsthorat commented May 3, 2018

Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


src/gradients.ts, line 146 at r1 (raw file):

Previously, nsthorat (Nikhil Thorat) wrote…

ah this is going to introduce a memory leak, but your obversation is absolutely correct. Can you do this with a tf.tidy() instead of the dispose?

To be clearer:

return tidy(() => {

  const {value, grads} = ENV.engine.gradients(() => f(...args), args, dy);
  // ...
  return grads;
});

Comments from Reviewable

@Lewuathe Lewuathe force-pushed the avoid-disposing-intermediate-tensor branch 3 times, most recently from 0666775 to 022180e Compare May 5, 2018 00:34
@dsmilkov
Copy link
Contributor

:lgtm_strong: Thanks! Rerunning travis since we had a strange test failure last time


Reviewed 1 of 2 files at r1.
Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


Comments from Reviewable

@dsmilkov
Copy link
Contributor

Reviewed 1 of 2 files at r2.
Review status: 1 of 2 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


src/tensor_test.ts, line 1093 at r2 (raw file):

  });

  it('grad with second derivative', () => {

can you move this test outside of the describe('tensor.toString') and inside the describeWithFlags('tensor', ALL_ENVS, () => { in the same file above since it does do mathematical operations. This is why the travis fails: https://api.travis-ci.org/v3/job/377450573/log.txt


Comments from Reviewable

@Lewuathe
Copy link
Contributor Author

Review status: 1 of 2 files reviewed at latest revision, 3 unresolved discussions, some commit checks failed.


src/tensor_test.ts, line 1093 at r2 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

can you move this test outside of the describe('tensor.toString') and inside the describeWithFlags('tensor', ALL_ENVS, () => { in the same file above since it does do mathematical operations. This is why the travis fails: https://api.travis-ci.org/v3/job/377450573/log.txt

Oh, good catch. I'm wondering why the test keeps failing. Thanks I'm going to fix it.


Comments from Reviewable

@Lewuathe Lewuathe force-pushed the avoid-disposing-intermediate-tensor branch from 1109618 to c361ee2 Compare May 11, 2018 21:50
@dsmilkov dsmilkov merged commit 566de98 into tensorflow:master May 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants