# How does GradientTape "record" operation

**URL:** <https://discuss.ai.google.dev/t/how-does-gradienttape-record-operation/30930>\
**Category:** General Discussion\
**Tags:** tfcore, help\_request\
**Created:** [February 7, 2023, 2:37am UTC](https://discuss.ai.google.dev/t/how-does-gradienttape-record-operation/30930 "2023-02-07T02:37:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nihal\_Kenkre](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@Nihal\_Kenkre](https://discuss.ai.google.dev/u/Nihal_Kenkre)\
**Post date:** [February 7, 2023, 2:37am UTC](https://discuss.ai.google.dev/t/how-does-gradienttape-record-operation/30930/1 "2023-02-07T02:37:44Z")

</div>

Hello.

Would like to know how exactly does the GradientTape context, record operations.

Browsing through github i followed the code to the C definition of the GradientTape PyObject, and functions to manipulate the tape and operations.

But what exactly triggers the recording.

The comments in the relevant files mention context / trace with regards to recording operations.

Any directions would be helpful.

Best regards.

---

<div class="post-metadata">

**Author:** ![Kiran\_Sai\_Ramineni](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/kiran_sai_ramineni/32/8859_2.png) [@Kiran\_Sai\_Ramineni](https://discuss.ai.google.dev/u/Kiran_Sai_Ramineni)\
**Post date:** [February 7, 2023, 7:02am UTC](https://discuss.ai.google.dev/t/how-does-gradienttape-record-operation/30930/3 "2023-02-07T07:02:45Z")

</div>

Hi @Nihal_Kenkre, recording of gradients in GradientTape starts when [tf.GradientTape.watch](https://github.com/tensorflow/tensorflow/blob/v2.11.0/tensorflow/python/eager/backprop.py#L911-L931) is called. Thank You.

---

<div class="post-metadata">

**Author:** ![Nihal\_Kenkre](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@Nihal\_Kenkre](https://discuss.ai.google.dev/u/Nihal_Kenkre)\
**Post date:** [February 7, 2023, 7:23am UTC](https://discuss.ai.google.dev/t/how-does-gradienttape-record-operation/30930/4 "2023-02-07T07:23:31Z")

</div>

Some more information on the question.

This is the GradientTape doc - [tf.GradientTape &nbsp;|&nbsp; TensorFlow v2.16.1](https://www.tensorflow.org/api_docs/python/tf/GradientTape)

It records the operations happening inside the GradientTape context.

Any pointers on how this is possible would be helpful.

There is python code for gradient tape which contains a tape class which then calls C extensions for GradientTape and Tape.

e.g. Tape Class

> <https://github.com/tensorflow/tensorflow/blob/9a4af32dae70849e8175c17b68f8627e926d28e4/tensorflow/c/eager/tape.h>

But i am not able to figure out the “Record Operations Trigger” which happens within the GradientTape context.

Is there a way to use the trace and traceback modules in python to do something like this? but how would they know where to start and stop.

Cheers
