arcadiaphy commented on a change in pull request #15007: Add matrix determinant
operator in linalg
URL: https://github.com/apache/incubator-mxnet/pull/15007#discussion_r287312933
##########
File path: src/operator/tensor/la_op.cc
##########
@@ -939,5 +939,153 @@ NNVM_REGISTER_OP(_backward_linalg_inverse)
.set_attr<nnvm::TIsBackward>("TIsBackward", true)
.set_attr<FCompute>("FCompute<cpu>", LaOpBackward<cpu, 2, 2, 2, 1,
inverse_backward>);
+NNVM_REGISTER_OP(_linalg_det)
+.add_alias("linalg_det")
+.describe(R"code(Compute the determinant of a matrix.
+Input is a tensor *A* of dimension *n >= 2*.
+
+If *n=2*, *A* is a square matrix. We compute:
+
+ *out* = *det(A)*
+
+If *n>2*, *det* is performed separately on the trailing two dimensions
+for all inputs (batch mode).
+
+.. note:: The operator supports float32 and float64 data types only.
+
+Examples::
+
+ Single matrix inversion
+ A = [[1., 4.], [2., 3.]]
+ det(A) = [-5.]
+
+ Batch matrix inversion
+ A = [[[1., 4.], [2., 3.]],
+ [[2., 3.], [1., 4.]]]
+ det(A) = [-5., 5.]
+)code" ADD_FILELINE)
+.set_num_inputs(1)
+.set_num_outputs(3)
+.set_attr<nnvm::FListInputNames>("FListInputNames", [](const NodeAttrs& attrs)
+ { return std::vector<std::string>{"A"}; })
+.set_attr<nnvm::FNumVisibleOutputs>("FNumVisibleOutputs", [](const NodeAttrs&
attrs) {
+ return 1; })
+.set_attr<mxnet::FInferShape>("FInferShape", DetShape<1>)
+.set_attr<nnvm::FInferType>("FInferType", DetType<1>)
+.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& attrs)
+ { return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; })
+.set_attr<FCompute>("FCompute<cpu>", LaOpDetForward<cpu, 1, det>)
+.set_attr<nnvm::FGradient>("FGradient",
ReduceDetGrad<1>{"_backward_linalg_det"})
+.add_argument("A", "NDArray-or-Symbol", "Tensor of square matrix");
+
+NNVM_REGISTER_OP(_backward_linalg_det)
+.set_num_inputs(4)
+.set_num_outputs(1)
+.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& attrs)
+ { return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; })
+.set_attr<nnvm::TIsBackward>("TIsBackward", true)
+.set_attr<FCompute>("FCompute<cpu>", LaOpDetBackward<cpu, 1, det_backward>);
+
+NNVM_REGISTER_OP(_linalg_logdet)
+.add_alias("linalg_logdet")
+.describe(R"code(Compute the log determinant of a matrix.
+Input is a tensor *A* of dimension *n >= 2*.
+
+If *n=2*, *A* is a square matrix. We compute:
+
+ *out* = *log(det(A))*
+
+If *n>2*, *logdet* is performed separately on the trailing two dimensions
+for all inputs (batch mode).
+
+.. note:: The operator supports float32 and float64 data types only.
+
+Examples::
+
+ Single matrix inversion
+ A = [[2., 3.], [1., 4.]]
+ logdet(A) = [1.609438]
+
+ Batch matrix inversion
+ A = [[[2., 3.], [1., 4.]],
+ [[1., 2.], [2., 4.]],
+ [[1., 2.], [4., 3.]]]
+ logdet(A) = [1.609438, -inf, nan]
+)code" ADD_FILELINE)
+.set_num_inputs(1)
+.set_num_outputs(3)
+.set_attr<nnvm::FListInputNames>("FListInputNames", [](const NodeAttrs& attrs)
+ { return std::vector<std::string>{"A"}; })
+.set_attr<nnvm::FNumVisibleOutputs>("FNumVisibleOutputs", [](const NodeAttrs&
attrs) {
+ return 1; })
+.set_attr<mxnet::FInferShape>("FInferShape", DetShape<1>)
+.set_attr<nnvm::FInferType>("FInferType", DetType<1>)
+.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& attrs)
+ { return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; })
+.set_attr<FCompute>("FCompute<cpu>", LaOpDetForward<cpu, 1, logdet>)
+.set_attr<nnvm::FGradient>("FGradient",
ReduceDetGrad<1>{"_backward_linalg_logdet"})
+.add_argument("A", "NDArray-or-Symbol", "Tensor of square matrix");
+
+NNVM_REGISTER_OP(_backward_linalg_logdet)
+.set_num_inputs(4)
+.set_num_outputs(1)
+.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& attrs)
+ { return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; })
+.set_attr<nnvm::TIsBackward>("TIsBackward", true)
+.set_attr<FCompute>("FCompute<cpu>", LaOpDetBackward<cpu, 1, logdet_backward>);
+
+NNVM_REGISTER_OP(_linalg_slogdet)
+.add_alias("linalg_slogdet")
+.describe(R"code(Compute the sign and log of the determinant of a matrix.
+Input is a tensor *A* of dimension *n >= 2*.
+
+If *n=2*, *A* is a square matrix. We compute:
+
+ *sign* = *sign(det(A))*
+ *logabsdet* = *log(abs(det(A)))*
+
+If *n>2*, *slogdet* is performed separately on the trailing two dimensions
+for all inputs (batch mode).
+
+.. note:: The operator supports float32 and float64 data types only.
+.. note:: The gradient is not properly defined on sign, so the gradient of
+ it is not backwarded.
+
+Examples::
+
+ Single matrix inversion
+ A = [[2., 3.], [1., 4.]]
+ sign, logabsdet = slogdet(A)
+ sign = [1.]
+ logabsdet = [1.609438]
+
+ Batch matrix inversion
+ A = [[[2., 3.], [1., 4.]],
+ [[1., 2.], [2., 4.]],
+ [[1., 2.], [4., 3.]]]
+ sign, logabsdet = slogdet(A)
+ sign = [1., 0., -1.]
+ logabsdet = [1.609438, -inf, 1.609438]
+)code" ADD_FILELINE)
+.set_num_inputs(1)
+.set_num_outputs(4)
+.set_attr<nnvm::FListInputNames>("FListInputNames", [](const NodeAttrs& attrs)
+ { return std::vector<std::string>{"A"}; })
+.set_attr<nnvm::FNumVisibleOutputs>("FNumVisibleOutputs", [](const NodeAttrs&
attrs) {
+ return 2; })
+.set_attr<mxnet::FInferShape>("FInferShape", DetShape<2>)
+.set_attr<nnvm::FInferType>("FInferType", DetType<2>)
Review comment:
If canonical type inference is used, then pivot tensor has different sizes
for float and double. I need to calculate how much DType space is enough to
hold pivot integers like what's done in workspace estimate. I think just use
int is more clear and simple here.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services