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

Added TensorFlow support to nncf.Tensor #3106

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

olegkkruglov
Copy link

Changes

  • Added tf_numeric.py and tf_linalg.py files with implementations of methods needed for nncf.Tensor support.
  • Added TensorFlow backend for nncf.Tensor.
  • Fixed bug in __ifloordiv__ operator for nncf.Tensor.
  • Added related tests.

Reason for changes

Currently TensorFlow tensors are not supported by nncf.Tensor. It prevents #3041 from being done.

Related tickets

#3041

Tests

TestTFNNCFTensorOperators and TestGPUTFNNCFTensorOperators classes were added to tests/tensorflow/test_tensor.py. Some changes were necessary for tests/cross_fw/test_templates/template_test_nncf_tensor.py, mostly related to different device management in TensorFlow.

@github-actions github-actions bot added the NNCF TF Pull requests that updates NNCF TensorFlow label Nov 22, 2024
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 22, 2024
@olegkkruglov olegkkruglov marked this pull request as ready for review November 22, 2024 17:01
@olegkkruglov olegkkruglov requested a review from a team as a code owner November 22, 2024 17:01
@kshpv kshpv requested a review from alexsu52 November 28, 2024 13:45
@alexsu52
Copy link
Contributor

alexsu52 commented Dec 6, 2024

Please, update your branch from develop.

@kshpv kshpv self-requested a review December 6, 2024 09:43
Copy link
Contributor

@alexsu52 alexsu52 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 contributing. Please address my comments.
PS: Sorry for delay with reviewing.

keepdims: bool = False,
) -> tf.Tensor:
numpy_a = np.array(a)
numpy_median = np.median(numpy_a, axis=axis, keepdims=keepdims)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to implement median using tf.math.top_k. I hope the better performance can be achieved on GPU using tf.math.top_k.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have any evidence that this implementation is more performant than calling np.median?

@kshpv
Copy link
Collaborator

kshpv commented Jan 20, 2025

It looks like the PR is stuck. I see that @olegkkruglov answered all the comments from @alexsu52
@alexsu52 did you have a chance to take a look at the updated PR?

Copy link
Contributor

@alexsu52 alexsu52 left a comment

Choose a reason for hiding this comment

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

Please, update your branch from develop.

axis = (0, 1)

with tf.device(a.device):
if ord == "nuc" and isinstance(axis, tuple) and len(axis) != 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, check that keepdims is supported in all cases. I found that this case does not support for keepdims=True.

Please also add a test for this case.

from nncf.tensor.functions import linalg


@linalg.norm.register(tf.Tensor)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add also implementation for ord=0.

Copy link
Contributor

@alexsu52 alexsu52 left a comment

Choose a reason for hiding this comment

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

Looks like the pull request is in the home stretch. Please address my comments and add support for as_numpy_tensor function from develop as well.

Comment on lines +78 to +121
with tf.device(a.device):
if driver is not None:
warnings.warn("Driver specifying is not supported in TensorFlow lstsq method")
if tf.rank(b) == 1:
b = tf.expand_dims(b, axis=0)
perm = list(range(tf.rank(b)))
perm[-1], perm[-2] = perm[-2], perm[-1]
b = tf.transpose(b, perm=perm)

return tf.linalg.lstsq(a, b)
Copy link
Contributor

Choose a reason for hiding this comment

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

I run test cases with the code from the suggestion and tests were passed:
image

Please add test when rank(b) > 2 for example:

import numpy as np

x = [1.0, 2.0, 4.0]
a = np.vstack([x, np.ones_like(x) ** 0]).transpose()
b = np.array([[6.0, 6.0], [8.0, 10.0], [12.0, 18.0]])
np.linalg.lstsq(a, b)[0]

Comment on lines 68 to 71
with tf.device(a.device):
if axis is None:
return tf.reduce_max(a)
return tf.reduce_max(a, axis=axis, keepdims=keepdim)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would ask you to fix torch in this PR as it is a bug. Thanks

@numeric.unstack.register(tf.Tensor)
def _(x: tf.Tensor, axis: int = 0) -> List[tf.Tensor]:
with tf.device(x.device):
if not list(x.shape):
Copy link
Contributor

Choose a reason for hiding this comment

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

@AlexanderDokuchaev, could you provide a code snippet when this if will be true?

keepdims: bool = False,
) -> tf.Tensor:
numpy_a = np.array(a)
numpy_median = np.median(numpy_a, axis=axis, keepdims=keepdims)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have any evidence that this implementation is more performant than calling np.median?



@numeric.round.register(tf.Tensor)
def _(a: tf.Tensor, decimals=0) -> tf.Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _(a: tf.Tensor, decimals=0) -> tf.Tensor:
def _(a: tf.Tensor, decimals: int = 0) -> tf.Tensor:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation NNCF TF Pull requests that updates NNCF TensorFlow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants