forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding quantized clamp kernel (pytorch#30541)
Summary: Pull Request resolved: pytorch#30541 ghstack-source-id: 95450749 Adding quantized clamp kernel Test Plan: Added test. buck test mode/dev //caffe2/test:quantized -- 'test_qclamp \(test_quantized\.TestQuantizedOps\)' --print-passing-details Differential Revision: D18739628 fbshipit-source-id: 38a029ab96c5b0689bb15c67dc4f274883e74975
- Loading branch information
1 parent
1d5af95
commit a2463cb
Showing
7 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <ATen/ATen.h> | ||
#include <ATen/NativeFunctions.h> | ||
#include <ATen/core/op_registration/op_registration.h> | ||
#include <ATen/native/TensorIterator.h> | ||
#include <ATen/native/cpu/Loops.h> | ||
#include <ATen/native/quantized/cpu/quantized_ops.h> | ||
#include <ATen/quantized/Quantizer.h> | ||
|
||
#include <algorithm> | ||
|
||
namespace at { | ||
namespace native { | ||
|
||
DEFINE_DISPATCH(qclamp_stub); | ||
|
||
namespace { | ||
Tensor quantized_clamp_impl( | ||
const Tensor& qx, | ||
optional<Scalar> min, | ||
optional<Scalar> max) { | ||
Tensor qy; | ||
if (min && max) { | ||
qclamp_stub(qx.device().type(), qx, *min, *max, qy); | ||
} else { | ||
TORCH_CHECK( | ||
false, "Both min and max should be specifed for quantized clamp!"); | ||
} | ||
return qy; | ||
} | ||
} // namespace | ||
|
||
// at::native functions for the native_functions.yaml | ||
Tensor quantized_clamp( | ||
const Tensor& qx, | ||
optional<Scalar> min, | ||
optional<Scalar> max) { | ||
Tensor qy; | ||
AT_DISPATCH_QINT_TYPES(qx.scalar_type(), "clamp", [&]() { | ||
qy = quantized_clamp_impl(qx, min, max); | ||
}); | ||
return qy; | ||
} | ||
|
||
// Keep the registry in the anonymous namespace. | ||
namespace { | ||
class QClamp final : public c10::OperatorKernel { | ||
public: | ||
Tensor operator()(Tensor qx, optional<Scalar> min, optional<Scalar> max) { | ||
return quantized_clamp(qx, min, max); | ||
} | ||
}; | ||
|
||
static auto registry = c10::RegisterOperators().op( | ||
"quantized::clamp(Tensor qx, Scalar? min, Scalar? max) -> Tensor qy", | ||
c10::RegisterOperators::options().kernel<QClamp>( | ||
TensorTypeId::QuantizedCPUTensorId)); | ||
} // namespace | ||
|
||
} // namespace native | ||
} // namespace at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters