For a tensorflow public function, is there a way to link the script that it is in and the function name?

Hi, new Tensorflow user here. I am studying Tensorflow source code and have a very specific question. For every public function, I want to get the source code equivalent of it. https://www.tensorflow.org/api_docs/python/tf/all_symbols I tried using this but in the source code, public functions are named differently compared to the API symbols.

Example:
image
This is in the python file tensor_util. tensorflow/tensor_util.py at master · tensorflow/tensorflow · GitHub

Is it possible to form the connection between source code and API symbols? For example, with the picture above, I want to connect tensor_util.constant_value to tf.get_static_value and retrieve all the source code equivalent of the public API symbols.

You can try using some of the helpers in tf_export.py:

from tensorflow.python.util.tf_export import get_symbol_from_name

get_symbol_from_name('get_static_value')

Returns:

<function tensorflow.python.framework.tensor_util.constant_value>

You can then use the functions __code__ property to extract whatever information you’re looking for. Here’s a function that prints out an API function’s name, module name, file name, and definition line number:

from tensorflow.python.util.tf_export import get_symbol_from_name

def print_api_info(api_name):
  func = get_symbol_from_name(api_name)
  if func is not None:
    module = func.__module__
    code_object = func.__code__
    fname = code_object.co_filename
    line_no = code_object.co_firstlineno
    name = code_object.co_name
    print(f'Name: {name}\nModule: {module}\nFile: {fname}\nLineNo: {line_no}\n')

Trying with your example:

print_api_info('get_static_value')

Prints:

Name: constant_value
Module: tensorflow.python.framework.tensor_util
File: /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py
LineNo: 809

EDIT: api_name should contain the full namespace within TensorFlow. e.g. for tf.autograph.set_verbosity, you would do:

print_api_info('autograph.set_verbosity')

I’m not certain if this will be useful for every function in the API, but it might be a start in navigating around the source.

2 Likes