altanh commented on a change in pull request #8056:
URL: https://github.com/apache/tvm/pull/8056#discussion_r634783404



##########
File path: src/relay/op/nn/nn.cc
##########
@@ -1091,6 +1092,65 @@ Accept logits.
 // Depth to space and space to depth
 TVM_REGISTER_NODE_TYPE(SubPixelAttrs);
 
+// relay.nn.nll_loss
+TVM_REGISTER_NODE_TYPE(NLLLossAttrs);
+
+bool NLLLossRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
+                const TypeReporter& reporter) {
+  ICHECK_EQ(types.size(), 4) << "NLLLossRel expects 4 types, but " << 
types.size()
+                             << " were provided.";
+  const auto* predictions = types[0].as<TensorTypeNode>();
+  const auto* targets = types[1].as<TensorTypeNode>();
+  const auto* weights = types[2].as<TensorTypeNode>();
+  const NLLLossAttrs* param = attrs.as<NLLLossAttrs>();
+  if (predictions == nullptr || targets == nullptr || weights == nullptr) 
return false;
+  ICHECK(predictions->shape.size() - targets->shape.size() == 1)
+      << "NLLLossRel: predictions should be one dimension larger than targets, 
"
+      << "predictions shape = " << predictions->shape << ", "
+      << "targets shape = " << targets->shape;
+  ICHECK(weights->shape.size() == 1)
+      << "NLLLossRel: weights should be a one dimension Tensor with its length 
"
+      << "the number of classes, but Tensor of dimension " << 
weights->shape.size()
+      << " were provided.";
+  ICHECK(reporter->AssertEQ(predictions->shape[1], weights->shape[0]))
+      << "NLLLossRel: the second dimension of predictions should be the number 
of classes, "
+      << "which is the length of weights, "
+      << "predictions shape = " << predictions->shape << ", "
+      << "weights shape = " << weights->shape;
+  ICHECK(predictions->dtype == weights->dtype && predictions->dtype.is_float())
+      << "NLLLossRel: predictions and weights should be of the same floating 
type.";
+  ICHECK(targets->dtype.is_int()) << "NLLLossRel: targets should be of int 
type.";

Review comment:
       can you replace these ICHECKs with Diagnostics?

##########
File path: include/tvm/topi/nn.h
##########
@@ -642,6 +643,54 @@ inline tvm::te::Tensor batch_to_space_nd(const 
tvm::te::Tensor& data,
   out = strided_slice(out, begin_idx, end_idx, strides);
   return out;
 }
+
+/*!
+ * \brief Negative log likelihood loss.
+ *
+ * \param predictions The prediction tensor.
+ * \param targets The target tensor.
+ * \param weights A manual rescaling weight given to each class.
+ * \param reduction The reduction method to apply to the output.
+ * \param ignore_index The target value to ignore.
+ * \param name The name of the operation.
+ * \param tag The tag to mark the operation.
+ *
+ * \return A Tensor whose op member is the batch_to_space_nd operation

Review comment:
       fix incorrect return docstring

##########
File path: python/tvm/relay/op/nn/nn.py
##########
@@ -2973,6 +2973,40 @@ def cross_entropy_with_logits(predictions, targets):
     return _make.cross_entropy_with_logits(predictions, targets)
 
 
+def nll_loss(predictions, targets, weights, reduction="mean", 
ignore_index=-100):
+    """Negative log likelihood loss.
+
+    output{n, i_1, i_2, ..., i_k} = predictions{n, t, i_1, i_2, i_k}
+      where t = target{n, i_1, i_2, ..., i_k}
+
+    result = reduction(output)
+
+    Parameters
+    ----------
+    predictions : tvm.relay.Expr
+      The predictions.
+
+    targets : tvm.relay.Expr
+      The target value of each prediction.
+
+    weights : tvm.relay.Expr

Review comment:
       can we make weights optional, like PyTorch? weights=1 is a pretty common 
case I believe and we could add a fast path implementation that skips the 
scaling

##########
File path: include/tvm/topi/nn.h
##########
@@ -642,6 +643,54 @@ inline tvm::te::Tensor batch_to_space_nd(const 
tvm::te::Tensor& data,
   out = strided_slice(out, begin_idx, end_idx, strides);
   return out;
 }
+
+/*!
+ * \brief Negative log likelihood loss.
+ *
+ * \param predictions The prediction tensor.
+ * \param targets The target tensor.
+ * \param weights A manual rescaling weight given to each class.
+ * \param reduction The reduction method to apply to the output.
+ * \param ignore_index The target value to ignore.
+ * \param name The name of the operation.
+ * \param tag The tag to mark the operation.
+ *
+ * \return A Tensor whose op member is the batch_to_space_nd operation
+ */
+inline Tensor nll_loss(const Tensor& predictions, const Tensor& targets, const 
Tensor& weights,
+                       std::string reduction = "mean", int ignore_index = -100,
+                       const std::string name = "nll_loss", const std::string 
tag = kBroadcast) {

Review comment:
       should the tag be `kOpaque` to match the Relay pattern?

##########
File path: python/tvm/topi/nn/loss.py
##########
@@ -0,0 +1,58 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name,unused-argument
+"""Loss functions definitions."""
+from __future__ import absolute_import
+from . import cpp
+
+
+def nll_loss(predictions, targets, weights, reduction, ignore_index):
+    """Negative log likelihood loss on the input data.
+
+    output{n, i_1, i_2, ..., i_k} = predictions{n, t, i_1, i_2, i_k}

Review comment:
       missing minus sign in math? and weights

##########
File path: include/tvm/topi/nn.h
##########
@@ -642,6 +643,54 @@ inline tvm::te::Tensor batch_to_space_nd(const 
tvm::te::Tensor& data,
   out = strided_slice(out, begin_idx, end_idx, strides);
   return out;
 }
+
+/*!
+ * \brief Negative log likelihood loss.
+ *
+ * \param predictions The prediction tensor.
+ * \param targets The target tensor.
+ * \param weights A manual rescaling weight given to each class.
+ * \param reduction The reduction method to apply to the output.
+ * \param ignore_index The target value to ignore.
+ * \param name The name of the operation.
+ * \param tag The tag to mark the operation.
+ *
+ * \return A Tensor whose op member is the batch_to_space_nd operation
+ */
+inline Tensor nll_loss(const Tensor& predictions, const Tensor& targets, const 
Tensor& weights,
+                       std::string reduction = "mean", int ignore_index = -100,
+                       const std::string name = "nll_loss", const std::string 
tag = kBroadcast) {
+  auto T = tvm::te::compute(
+      targets->shape,
+      [&](const tvm::Array<tvm::tir::Var>& target_indices) {
+        auto c = targets(target_indices);
+        tvm::Array<tvm::PrimExpr> pred_indices;
+        for (size_t i = 0; i < target_indices.size(); i++) {
+          pred_indices.push_back(target_indices[i]);
+          if (i == 0) {
+            pred_indices.push_back(c);
+          }
+        }

Review comment:
       This is clearer to me
   ```suggestion
           pred_indices.push_back(target_indices[0]);  // batch index
           pred_indices.push_back(c); // class index
           for (size_t i = 1; i < target_indices.size(); i++) {
             pred_indices.push_back(target_indices[i]);  // indices for 
multidimensional loss
           }
   ```

##########
File path: python/tvm/relay/op/nn/nn.py
##########
@@ -2973,6 +2973,40 @@ def cross_entropy_with_logits(predictions, targets):
     return _make.cross_entropy_with_logits(predictions, targets)
 
 
+def nll_loss(predictions, targets, weights, reduction="mean", 
ignore_index=-100):
+    """Negative log likelihood loss.
+
+    output{n, i_1, i_2, ..., i_k} = predictions{n, t, i_1, i_2, i_k}

Review comment:
       missing a minus sign in the math 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]


Reply via email to