Skip to content

[AdvancedCompiler] Fill(cpp wrapper) #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

AdvancedCompiler
Copy link
Contributor

PR Category

Operator

Type of Change

New Feature

Description

CPP wrapper packaging for fill op.

Issue

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

lib/fill.cpp Outdated
using namespace triton_jit;

// ----------- scalar fill:创建新张量并用标量填充 -----------
at::Tensor fill_scalar(const at::Tensor& input, double value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aten's fill has schema

- fill.Scalar(Tensor self, Scalar value) -> Tensor
- fill.Tensor(Tensor self, Tensor value) -> Tensor
- fill_.Scalar(Tensor(a!) self, Scalar value) -> Tensor(a!)
- fill_.Tensor(Tensor(a!) self, Tensor value) -> Tensor(a!)

c10::Scalar is torch's type for a numeric scalar whose type is not known at compile time. It is common for torch to use a macro to expand the cases with its underlaying dtype.

Tensor& scalar_fill(Tensor& self, const Scalar& value) {
  AT_DISPATCH_V2(
      self.scalar_type(), "fill_out", AT_WRAP([&]() {
        fill_inplace<scalar_t>(self, value);
      }), kComplexHalf, kHalf, kBool, kBFloat16, AT_EXPAND(AT_ALL_TYPES_AND_COMPLEX), AT_EXPAND(AT_FLOAT8_TYPES), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES));
  return self;
}

We can also use these macros, but it may be a little hard to get it right.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should try to get it work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scalar type is not same as basic type like float?

@iclementine iclementine self-assigned this Jul 11, 2025
Copy link
Collaborator

@Bowen12992 Bowen12992 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

lib/fill.cpp Outdated
using namespace triton_jit;

// ----------- scalar fill:创建新张量并用标量填充 -----------
at::Tensor fill_scalar(const at::Tensor& input, double value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scalar type is not same as basic type like float?

@@ -31,5 +30,16 @@ std::tuple<at::Tensor, at::Tensor> rotary_embedding(
const std::optional<at::Tensor> &position_ids = std::nullopt,
bool rotary_interleaved = false);

at::Tensor fill_scalar(const at::Tensor &input, c10::Scalar value);
Copy link
Collaborator

@iclementine iclementine Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const c10::Scalar& for argument type Scalar in native_functions.yaml since it is the convention.

Check aten/src/ATen/native/Fill.cpp for details. We should follow the signature.

Tensor& fill_(Tensor& self, const Scalar& value);
Tensor& fill_(Tensor& self, const Tensor& value);
Tensor fill(const Tensor& self, const Scalar& value);
Tensor fill(const Tensor& self, const Tensor& value);

@@ -22,6 +22,11 @@ TORCH_LIBRARY(flag_gems, m) {
"rotary_embedding(Tensor q, Tensor k, Tensor cos, Tensor sin, Tensor? position_ids=None, "
"bool rotary_interleaved=False) -> (Tensor, Tensor)"); // q and k may be view to other size
m.def("bmm(Tensor self, Tensor mat2) -> Tensor");
// fill operator declaration
m.def("fill_scalar(Tensor input, double value) -> Tensor");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be consistent with the function signature, use Scalar for fill_scalar.

Also, we suggest

  1. using the same signature as in Aten, since we may also register it for ATen in the future.
  2. use marker to mark aliasing, and ! to mark mutation.

fill.Scalar and fill.Tensor has the same op name with different overload.

fill.Scalar(Tensor self, Scalar value) -> Tensor
fill.Tensor(Tensor self, Tensor value) -> Tensor
fill_.Scalar(Tensor(a!) self, Scalar value) -> Tensor(a!)
fill_.Tensor(Tensor(a!) self, Tensor value) -> Tensor(a!)

Copy link
Collaborator

@iclementine iclementine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check the function signature of ATen's corresponding functions.

@StrongSpoon
Copy link
Collaborator

Since pr #800 has been merged, I will close this pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants