Skip to content

Commit 9c0db34

Browse files
fionalangtensorflower-gardener
authored andcommitted
Replace solve registry with methods on LinearOperator and its subclasses.
PiperOrigin-RevId: 556889599
1 parent 1351be6 commit 9c0db34

16 files changed

Lines changed: 194 additions & 472 deletions

‎tensorflow/python/kernel_tests/linalg/BUILD‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,6 @@ cuda_py_strict_test(
183183
],
184184
)
185185

186-
cuda_py_strict_test(
187-
name = "linear_operator_algebra_test",
188-
size = "small",
189-
srcs = ["linear_operator_algebra_test.py"],
190-
deps = [
191-
"//tensorflow/python/framework:tensor_shape",
192-
"//tensorflow/python/ops/linalg:linear_operator",
193-
"//tensorflow/python/ops/linalg:linear_operator_algebra",
194-
"//tensorflow/python/ops/linalg:solve_registrations",
195-
"//tensorflow/python/platform:client_testlib",
196-
],
197-
)
198-
199186
cuda_py_strict_test(
200187
name = "linear_operator_block_diag_test",
201188
size = "medium",

‎tensorflow/python/kernel_tests/linalg/linear_operator_algebra_test.py‎

Lines changed: 0 additions & 79 deletions
This file was deleted.

‎tensorflow/python/kernel_tests/linalg/linear_operator_inversion_test.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ def test_supplied_hint_used(self):
9797
self.assertTrue(operator_inv.is_non_singular)
9898
self.assertFalse(operator_inv.is_self_adjoint)
9999

100+
def test_solve_on_inverse(self):
101+
# The matrix values do not effect auto-setting of the flags.
102+
matrix = [[1., 0.], [1., 1.]]
103+
operator = linalg.LinearOperatorFullMatrix(matrix)
104+
operator_inv = operator.inverse()
105+
solved_result = operator_inv.solve(operator)
106+
self.assertIsInstance(
107+
solved_result, linalg.LinearOperatorComposition)
108+
100109
def test_inverse_of_inverse(self):
101110
# The matrix values do not effect auto-setting of the flags.
102111
matrix = [[1., 0.], [1., 1.]]

‎tensorflow/python/ops/BUILD‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,8 +2859,6 @@ py_strict_library(
28592859
"//tensorflow/python/autograph",
28602860
"//tensorflow/python/compiler/tensorrt:trt_convert_py",
28612861
"//tensorflow/python/eager:wrap_function",
2862-
"//tensorflow/python/ops/linalg:linear_operator_algebra",
2863-
"//tensorflow/python/ops/linalg:solve_registrations",
28642862
"//tensorflow/python/ops/parallel_for:control_flow_ops",
28652863
"//tensorflow/python/ops/ragged:ragged_batch_gather_ops",
28662864
"//tensorflow/python/ops/ragged:ragged_batch_gather_with_default_op",

‎tensorflow/python/ops/linalg/BUILD‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ py_strict_library(
150150
":linalg_impl",
151151
":linear_operator",
152152
":linear_operator_addition",
153-
":linear_operator_algebra",
154153
":linear_operator_full_matrix",
155154
":linear_operator_identity",
156155
":linear_operator_util",
@@ -180,7 +179,6 @@ py_strict_library(
180179
],
181180
deps = [
182181
":linalg_impl",
183-
":linear_operator_algebra",
184182
":linear_operator_util",
185183
":property_hint_util",
186184
":slicing",
@@ -215,15 +213,6 @@ py_strict_library(
215213
],
216214
)
217215

218-
py_strict_library(
219-
name = "linear_operator_algebra",
220-
srcs = ["linear_operator_algebra.py"],
221-
deps = [
222-
"//tensorflow/python/framework:ops",
223-
"//tensorflow/python/util:tf_inspect",
224-
],
225-
)
226-
227216
py_strict_library(
228217
name = "linear_operator_tridiag",
229218
srcs = ["linear_operator_tridiag.py"],
@@ -267,7 +256,6 @@ py_strict_library(
267256
srcs = ["linear_operator_block_diag.py"],
268257
deps = [
269258
":linear_operator",
270-
":linear_operator_algebra",
271259
":linear_operator_util",
272260
":property_hint_util",
273261
"//tensorflow/python/framework:common_shapes",
@@ -340,7 +328,6 @@ py_strict_library(
340328
deps = [
341329
":linalg_impl",
342330
":linear_operator",
343-
":linear_operator_algebra",
344331
":linear_operator_block_diag",
345332
":linear_operator_block_lower_triangular",
346333
":linear_operator_circulant",
@@ -354,7 +341,6 @@ py_strict_library(
354341
":linear_operator_toeplitz",
355342
":linear_operator_tridiag",
356343
":linear_operator_zeros",
357-
":solve_registrations",
358344
],
359345
)
360346

@@ -390,20 +376,6 @@ py_strict_library(
390376
],
391377
)
392378

393-
py_strict_library(
394-
name = "solve_registrations",
395-
srcs = ["solve_registrations.py"],
396-
deps = [
397-
":linear_operator",
398-
":linear_operator_algebra",
399-
":linear_operator_block_diag",
400-
":linear_operator_circulant",
401-
":linear_operator_diag",
402-
":linear_operator_identity",
403-
":property_hint_util",
404-
],
405-
)
406-
407379
py_strict_library(
408380
name = "slicing",
409381
srcs = ["slicing.py"],

‎tensorflow/python/ops/linalg/linalg.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
# go/tf-wildcard-import
1818
# pylint: disable=wildcard-import,unused-import
19-
from tensorflow.python.ops.linalg import linear_operator_algebra as _linear_operator_algebra
20-
from tensorflow.python.ops.linalg import solve_registrations as _solve_registrations
2119
from tensorflow.python.ops.linalg.linalg_impl import *
2220
from tensorflow.python.ops.linalg.linear_operator import *
2321
from tensorflow.python.ops.linalg.linear_operator_adjoint import *

‎tensorflow/python/ops/linalg/linear_operator.py‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from tensorflow.python.ops import resource_variable_ops
3838
from tensorflow.python.ops import variables
3939
from tensorflow.python.ops.linalg import linalg_impl as linalg
40-
from tensorflow.python.ops.linalg import linear_operator_algebra
4140
from tensorflow.python.ops.linalg import linear_operator_util
4241
from tensorflow.python.ops.linalg import property_hint_util
4342
from tensorflow.python.ops.linalg import slicing
@@ -963,7 +962,7 @@ def solve(self, rhs, adjoint=False, adjoint_arg=False, name="solve"):
963962
" {} but got {}.".format(
964963
left_operator.domain_dimension, right_operator.range_dimension))
965964
with self._name_scope(name): # pylint: disable=not-callable
966-
return linear_operator_algebra.solve(left_operator, right_operator)
965+
return self._linop_solve(left_operator, right_operator)
967966

968967
with self._name_scope(name): # pylint: disable=not-callable
969968
rhs = tensor_conversion.convert_to_tensor_v2_with_dispatch(
@@ -979,6 +978,48 @@ def solve(self, rhs, adjoint=False, adjoint_arg=False, name="solve"):
979978

980979
return self._solve(rhs, adjoint=adjoint, adjoint_arg=adjoint_arg)
981980

981+
def _linop_solve(
982+
self, left_operator: "LinearOperator", right_operator: "LinearOperator"
983+
) -> "LinearOperator":
984+
# instance of linear_operator_identity.LinearOperatorIdentity
985+
if hasattr(right_operator, "_ones_diag") and not hasattr(
986+
right_operator, "multiplier"
987+
):
988+
return left_operator.inverse()
989+
990+
# Generic solve of two `LinearOperator`s.
991+
is_square = property_hint_util.is_square(left_operator, right_operator)
992+
is_non_singular = None
993+
is_self_adjoint = None
994+
is_positive_definite = None
995+
996+
if is_square:
997+
is_non_singular = property_hint_util.combined_non_singular_hint(
998+
left_operator, right_operator
999+
)
1000+
elif is_square is False: # pylint:disable=g-bool-id-comparison
1001+
is_non_singular = False
1002+
is_self_adjoint = False
1003+
is_positive_definite = False
1004+
1005+
# LinearOperator outputs a LinearOperatorComposition instance that contains
1006+
# a LinearOperatorInversion instance, both of which
1007+
# inherit from LinearOperator. The inline import is necessary to avoid
1008+
# errors due to this cyclic dependency.
1009+
from tensorflow.python.ops.linalg import linear_operator_composition # pylint: disable=g-import-not-at-top
1010+
from tensorflow.python.ops.linalg import linear_operator_inversion # pylint: disable=g-import-not-at-top
1011+
1012+
return linear_operator_composition.LinearOperatorComposition(
1013+
operators=[
1014+
linear_operator_inversion.LinearOperatorInversion(left_operator),
1015+
right_operator,
1016+
],
1017+
is_non_singular=is_non_singular,
1018+
is_self_adjoint=is_self_adjoint,
1019+
is_positive_definite=is_positive_definite,
1020+
is_square=is_square,
1021+
)
1022+
9821023
def _solvevec(self, rhs, adjoint=False):
9831024
"""Default implementation of _solvevec."""
9841025
rhs_mat = array_ops.expand_dims(rhs, axis=-1)

‎tensorflow/python/ops/linalg/linear_operator_algebra.py‎

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)