mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/61361 This PR ports the `clamp` kernel to the structured format. In addition, it introduces `OptionalScalarRef` as a replacement for `c10::optional<Scalar>&`. The latter, although it is a reference type, can still involve copying the contained `Scalar` (e.g. if the actual parameter is a `Scalar` or if a `c10::optional<Scalar>` is constructed just to call a kernel). `OptionalScalarRef` contains only a `const Scalar&`, and stores flag about whether the instance contains something inside the `Scalar` itself using a new tag. For more information, see #55070. Test Plan: Imported from OSS Reviewed By: albanD Differential Revision: D29821533 Pulled By: SplitInfinity fbshipit-source-id: 88d55df5a4b2c14b68a57e4905d90eea1b088d99
32 lines
521 B
C++
32 lines
521 B
C++
#pragma once
|
|
|
|
namespace c10 {
|
|
|
|
template <typename T>
|
|
class OptionalRef {
|
|
public:
|
|
OptionalRef() : data_(nullptr) {}
|
|
OptionalRef(const T* data) : data_(data) {
|
|
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(data_);
|
|
}
|
|
OptionalRef(const T& data) : data_(&data) {}
|
|
|
|
bool has_value() const {
|
|
return data_ != nullptr;
|
|
}
|
|
|
|
const T& get() const {
|
|
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(data_);
|
|
return *data_;
|
|
}
|
|
|
|
operator bool() const {
|
|
return has_value();
|
|
}
|
|
|
|
private:
|
|
const T* data_;
|
|
};
|
|
|
|
} // namespace c10
|