Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a9b3d90
support class center sample of PartialFC
GuoxiaWang Jul 12, 2021
689a30e
add files for op
GuoxiaWang Jul 12, 2021
6e72978
add test_parallel_class_center_sample to CMakeLists.txt
GuoxiaWang Jul 13, 2021
678362c
skip test_class_center_sample_op when core is compiled with ROCM
GuoxiaWang Jul 13, 2021
61b968e
fix import core
GuoxiaWang Jul 13, 2021
51c52ed
adaptive rocm and fix random seed
GuoxiaWang Jul 15, 2021
f3de336
fix example code check
GuoxiaWang Jul 16, 2021
b5e4470
add fix_seed unittest
GuoxiaWang Jul 16, 2021
e8712d2
fix typo
GuoxiaWang Jul 16, 2021
06ebc9d
add unittests
GuoxiaWang Jul 16, 2021
7322b4b
fix the requesting changes according reviewer
GuoxiaWang Jul 19, 2021
b4b4239
fix class center sample bug
GuoxiaWang Jul 26, 2021
9e081a2
fix unittest
GuoxiaWang Jul 30, 2021
681c72b
Merge remote-tracking branch 'upstream/develop' into feature-class_ce…
GuoxiaWang Aug 10, 2021
c415737
rename paddle.class_center_sample to paddle.nn.functional.class_cente…
GuoxiaWang Aug 10, 2021
549a1cf
fix example format style
GuoxiaWang Aug 11, 2021
98f3a2b
fix example format style again
GuoxiaWang Aug 11, 2021
3999107
fix example format style again x2
GuoxiaWang Aug 11, 2021
3d597e1
fix example format style again x3
GuoxiaWang Aug 11, 2021
e19d8ca
fix example format style again x4
GuoxiaWang Aug 11, 2021
774b75b
fix example format style again x5
GuoxiaWang Aug 12, 2021
3d495a5
move to common.py and fix examples
GuoxiaWang Aug 12, 2021
aea56ca
Merge branch 'develop' into feature-class_center_sample
GuoxiaWang Aug 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions paddle/fluid/operators/class_center_sample_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.

#include "paddle/fluid/operators/class_center_sample_op.h"

namespace paddle {
namespace operators {

class ClassCenterSampleOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("Label"), "Input", "Label",
"ClassCenterSample");
OP_INOUT_CHECK(ctx->HasOutput("RemappedLabel"), "Output", "RemappedLabel",
"ClassCenterSample");
OP_INOUT_CHECK(ctx->HasOutput("SampledLocalClassCenter"), "Output",
"SampledLocalClassCenter", "ClassCenterSample");

auto x_dims = ctx->GetInputDim("Label");
PADDLE_ENFORCE_EQ(x_dims.size(), 1,
platform::errors::InvalidArgument(
"Rank of Input(Label) should be equal to 1, "
"but the value given is %d.",
x_dims.size()));

ctx->SetOutputDim("RemappedLabel", x_dims);
auto num_samples = ctx->Attrs().Get<int>("num_samples");
ctx->SetOutputDim("SampledLocalClassCenter",
framework::make_ddim({num_samples}));
}

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
OperatorWithKernel::IndicateVarDataType(ctx, "Label"),
ctx.device_context());
}
};

class ClassCenterSampleOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput(
"Label",
"(Tensor<int|int64>) The input of ClassCenterSample op. Each value "
"of Label is an integer label.");
AddOutput("RemappedLabel",
"(Tensor<int|int64>) Output tensor with same shape as Label. "
"Each label is remap using sampled class.");
AddOutput("SampledLocalClassCenter",
"(Tensor<int|int64>) The sampled class center for local rank,"
"value in [0, num_classes).");
AddAttr<int>(
"num_classes",
"A positive integer to specify the number of classes at local rank. "
"Note that num_classes of each GPU can be different.");
AddAttr<int>(
"num_samples",
"A positive integer to specify the number of class center to sample.");
AddAttr<int>("ring_id", "(int default 0) nccl communication ring id.")
.SetDefault(0);
AddAttr<int>("nranks", "(int default 1) The total number of GPUs.")
.SetDefault(1);
AddAttr<int>("rank", "(int default 0) The rank id in nranks.")
.SetDefault(0);
AddAttr<bool>("fix_seed",
"A flag indicating whether to use a fixed seed to generate "
"random negative class center. NOTE: DO NOT set this flag to"
"true in training. Setting this flag to true is only useful "
"in unittest or for debug")
.SetDefault(false);
AddAttr<int>("seed",
"Random seed used to generate random negative class center. "
"[default 0].")
.SetDefault(0);
AddComment(R"DOC(
Class center sample method is proposed from the paper PartialFC that only sample a subset of the class centers.
The process of sampling subset class centers is straightforward: 1) First select the positive class centers;
2) Randomly sample negative class centers. Specifically, given a Label tensor, shape [batch_size], select all
the positive class centers and randomly sample negative class centers, then remap the input label tensor using
the sampled class centers. Note that if the number of the positive class centers is greater than the input
num_samples, it keeps all the positive class centers and the shape of SampledLocalClassCenter will be
[num_positive_class_centers]. The op supports CPU, single GPU and multi GPU.

For more information, Partial FC: Training 10 Million Identities on a Single Machine
arxiv: https://arxiv.org/abs/2010.05222

Examples:
For CPU or only one GPU
Given:
Label: [11, 5 , 1 , 3 , 12, 2 , 15, 19, 18, 19]
num_classes = 20
num_samples = 6
Then:
RemappedLabel: [4, 3, 0, 2, 5, 1, 6, 8, 7, 8]
SampledLocalClassCenter: [1 , 2 , 3 , 5 , 11, 12, 15, 18, 19]

For multi GPU
Given:
rank0:
Label: [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]
num_classes = 10
num_samples = 6
ring_id = 0
nranks = 2
rank = 0
rank1:
Label: [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]
num_classes = 10
num_samples = 6
ring_id = 0
nranks = 2
rank = 1
Then:
rank0:
RemappedLabel: [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]
SampledLocalClassCenter: [0, 2, 4, 8, 9, 3]
rank1:
RemappedLabel: [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]
SampledLocalClassCenter: [0, 1, 2, 3, 5, 7, 8]
)DOC");
}
};

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_WITHOUT_GRADIENT(class_center_sample, ops::ClassCenterSampleOp,
ops::ClassCenterSampleOpMaker);
REGISTER_OP_CPU_KERNEL(class_center_sample,
ops::ClassCenterSampleCPUKernel<int64_t>,
ops::ClassCenterSampleCPUKernel<int>);
Loading