[c++] Distance-agnostic triplet margin loss (#45377)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45377

This PR adds a C++ implementation of the TripletMarginWithDistanceLoss, for which the Python implementation was introduced in PR #43680.  It's based on PR #44072, but I'm resubmitting this to unlink it from Phabricator.

Test Plan: Imported from OSS

Reviewed By: izdeby

Differential Revision: D24003973

fbshipit-source-id: 2d9ada7260a6f27425ff2fdbbf623dad0fb79405
This commit is contained in:
Xinyu Li
2020-09-30 12:35:08 -07:00
committed by Facebook GitHub Bot
parent 181afd5220
commit c9bb990707
7 changed files with 379 additions and 11 deletions
+125 -2
View File
@@ -2085,6 +2085,115 @@ TEST_F(ModulesTest, TripletMarginLoss) {
ASSERT_EQ(anchor.sizes(), anchor.grad().sizes());
}
TEST_F(ModulesTest, TripletMarginWithDistanceLossDefaultParity) {
// Check that if we use torch::pairwise_distance with the default
// TripletMarginLoss options as our distance function, the outputs
// are equal (i.e., equal under defaults).
std::vector<TripletMarginWithDistanceLossOptions::reduction_t>
reductions = {torch::kSum, torch::kMean, torch::kNone};
std::vector<float> margins = {0.5, 1.0, 1.5};
std::vector<bool> swaps = {true, false};
for (auto& reduction : reductions) {
for (auto& margin : margins) {
for (const auto& swap : swaps) {
auto anchor =
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
auto positive =
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
auto negative =
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
auto basicOptions = TripletMarginLossOptions()
.reduction(reduction)
.margin(margin)
.swap(swap);
auto distanceOptions =
TripletMarginWithDistanceLossOptions()
.reduction(reduction)
.margin(margin)
.swap(swap);
TripletMarginLoss basicLoss(basicOptions);
TripletMarginWithDistanceLoss distanceLoss(distanceOptions);
auto basicOutput = basicLoss->forward(anchor, positive, negative);
auto distanceOutput = distanceLoss->forward(anchor, positive, negative);
auto basicOperatorOutput = basicLoss(anchor, positive, negative);
auto distanceOperatorOutput = distanceLoss(anchor, positive, negative);
ASSERT_TRUE(distanceOutput.allclose(basicOutput, 1e-6, 1e-6));
ASSERT_TRUE(distanceOperatorOutput.allclose(distanceOutput, 1e-6, 1e-6));
ASSERT_TRUE(distanceOperatorOutput.allclose(basicOperatorOutput, 1e-6, 1e-6));
// handle for torch::kNone reduction
auto sum = distanceOutput.sum();
sum.backward();
ASSERT_EQ(anchor.sizes(), anchor.grad().sizes());
ASSERT_EQ(positive.sizes(), positive.grad().sizes());
ASSERT_EQ(negative.sizes(), negative.grad().sizes());
}
}
}
}
TEST_F(ModulesTest, TripletMarginWithDistanceLossFunctionalParity) {
// Check for parity between F::triplet_margin_with_distance_loss and
// TripletMarginWithDistanceLoss.
auto pairwise_distance = [&](const torch::Tensor& x, const torch::Tensor& y) {
return torch::pairwise_distance(x, y);
};
auto cosine_distance = [&](const torch::Tensor& x,
const torch::Tensor& y) {
return 1.0 - torch::cosine_similarity(x, y);
};
std::vector<TripletMarginWithDistanceLossOptions::distance_function_t>
distance_functions = {pairwise_distance, cosine_distance};
std::vector<TripletMarginWithDistanceLossOptions::reduction_t>
reductions = {torch::kSum, torch::kMean, torch::kNone};
std::vector<float> margins = {0.5, 1.0, 1.5};
std::vector<bool> swaps = {true, false};
for (auto& function : distance_functions) {
for (auto& reduction : reductions) {
for (auto& margin : margins) {
for (const auto& swap : swaps) {
auto moduleOptions =
TripletMarginWithDistanceLossOptions()
.distance_function(function)
.reduction(reduction)
.margin(margin)
.swap(swap);
auto functionOptions =
torch::nn::functional::TripletMarginWithDistanceLossFuncOptions()
.distance_function(function)
.reduction(reduction)
.margin(margin)
.swap(swap);
auto anchor = torch::randn(
{100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
auto positive = torch::randn(
{100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
auto negative = torch::randn(
{100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
TripletMarginWithDistanceLoss distanceLoss(moduleOptions);
auto moduleOutput = distanceLoss->forward(anchor, positive, negative);
auto moduleOperatorOutput = distanceLoss(anchor, positive, negative);
auto functionOutput = torch::nn::functional::triplet_margin_with_distance_loss(
anchor, positive, negative, functionOptions);
ASSERT_TRUE(moduleOutput.allclose(functionOutput, 1e-6, 1e-6));
ASSERT_TRUE(moduleOperatorOutput.allclose(functionOutput, 1e-6, 1e-6));
}
}
}
}
}
TEST_F(ModulesTest, NLLLoss) {
NLLLoss loss;
auto input = torch::tensor({{-0.1315, -3.1315, -2.5315},
@@ -3529,9 +3638,9 @@ TEST_F(ModulesTest, PrettyPrintIdentity) {
}
TEST_F(ModulesTest, PrettyPrintFlatten) {
ASSERT_EQ(c10::str(Flatten()),
ASSERT_EQ(c10::str(Flatten()),
"torch::nn::Flatten(start_dim=1, end_dim=-1)");
ASSERT_EQ(c10::str(Flatten(FlattenOptions().start_dim(2).end_dim(4))),
ASSERT_EQ(c10::str(Flatten(FlattenOptions().start_dim(2).end_dim(4))),
"torch::nn::Flatten(start_dim=2, end_dim=4)");
}
@@ -4394,6 +4503,20 @@ TEST_F(ModulesTest, PrettyPrintTripletMarginLoss) {
"torch::nn::TripletMarginLoss(margin=3, p=2, eps=1e-06, swap=false)");
}
TEST_F(ModulesTest, PrettyPrintTripletMarginWithDistanceLoss) {
auto distanceOptions = TripletMarginWithDistanceLossOptions()
.distance_function([&](const torch::Tensor& x,
const torch::Tensor& y) {
return torch::pairwise_distance(x, y, 2.0, 1e-6);
})
.margin(1.5)
.swap(true)
.reduction(torch::kMean);
ASSERT_EQ(
c10::str(TripletMarginWithDistanceLoss(distanceOptions)),
"torch::nn::TripletMarginWithDistanceLoss(margin=1.5, swap=true)");
}
TEST_F(ModulesTest, PrettyPrintNLLLoss) {
ASSERT_EQ(
c10::str(NLLLoss()), "torch::nn::NLLLoss()");