Skip to content
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

Codegen amin amax #3771

Merged
merged 2 commits into from
Aug 11, 2022
Merged

Codegen amin amax #3771

merged 2 commits into from
Aug 11, 2022

Conversation

JackCaoG
Copy link
Collaborator

@JackCaoG JackCaoG commented Jul 26, 2022

fix

example of codegen ops that takes bool and vector<int64>

currently C++ test failed with

354] MLIR V1 optimization pass is not enabled
2022-07-26 05:12:14.133348: I 1334741 tensorflow/compiler/jit/xla_device.cc:429] XLA_GPU and XLA_CPU devices are deprecated and will be removed in subsequent releases. Instead, use either @tf.function(jit_compile=True) for must-compile semantics, or run with TF_XLA_FLAGS=--tf_xla_auto_jit=2 for auto-clustering best-effort compilation.
unknown file: Failure
C++ exception with description "`InlinedVector::at(size_type) const` failed bounds check" thrown in the test body.

XLANativeFunction.cpp

at::Tensor XLANativeFunctions::amax(const at::Tensor& self, at::IntArrayRef dim,
                                    bool keepdim) {
  XLA_FN_COUNTER("xla::");
  auto common_device = torch_xla::bridge::GetXlaDevice(self);
  TORCH_INTERNAL_ASSERT(common_device);

  torch_xla::XLATensorPtr lazy_self =
      torch_xla::bridge::GetXlaTensorOrCreateForWrappedNumber(self,
                                                              *common_device);
  torch::lazy::NodePtr node = torch::lazy::ReuseNode<Amax>(
      lazy_self->GetIrValue(), std::vector<int64_t>(dim.begin(), dim.end()),
      keepdim);
  if (!node) {
    auto self_meta = to_meta(self);
    auto out_meta = at::meta::amax(self_meta, dim, keepdim);

    std::vector<torch::lazy::Shape> shapes{
        torch::lazy::Shape(out_meta.scalar_type(), out_meta.sizes().vec())};
    TORCH_INTERNAL_ASSERT(shapes.size() == 1);
    if (torch::lazy::symbolicShapeEnabled()) {
      std::vector<torch::jit::IValue> inputs = {self, dim, keepdim};
      const char* schema_str =
          "aten::amax(Tensor self, int[1] dim=[], bool keepdim=False) -> "
          "Tensor";
      applySymbolicShapesOnLT(schema_str, inputs, shapes);
    }

    node = torch::lazy::MakeNode<Amax>(
        lazy_self->GetIrValue(), std::vector<int64_t>(dim.begin(), dim.end()),
        keepdim, std::move(shapes));
    CacheNode(node);
  }

  auto result = torch_xla::bridge::AtenFromXlaTensor(
      torch_xla::XLATensor::Create(std::move(node), *common_device));
  return result;
};

at::Tensor XLANativeFunctions::amin(const at::Tensor& self, at::IntArrayRef dim,
                                    bool keepdim) {
  XLA_FN_COUNTER("xla::");
  auto common_device = torch_xla::bridge::GetXlaDevice(self);
  TORCH_INTERNAL_ASSERT(common_device);

  torch_xla::XLATensorPtr lazy_self =
      torch_xla::bridge::GetXlaTensorOrCreateForWrappedNumber(self,
                                                              *common_device);
  torch::lazy::NodePtr node = torch::lazy::ReuseNode<Amin>(
      lazy_self->GetIrValue(), std::vector<int64_t>(dim.begin(), dim.end()),
      keepdim);
  if (!node) {
    auto self_meta = to_meta(self);
    auto out_meta = at::meta::amin(self_meta, dim, keepdim);

    std::vector<torch::lazy::Shape> shapes{
        torch::lazy::Shape(out_meta.scalar_type(), out_meta.sizes().vec())};
    TORCH_INTERNAL_ASSERT(shapes.size() == 1);
    if (torch::lazy::symbolicShapeEnabled()) {
      std::vector<torch::jit::IValue> inputs = {self, dim, keepdim};
      const char* schema_str =
          "aten::amin(Tensor self, int[1] dim=[], bool keepdim=False) -> "
          "Tensor";
      applySymbolicShapesOnLT(schema_str, inputs, shapes);
    }

    node = torch::lazy::MakeNode<Amin>(
        lazy_self->GetIrValue(), std::vector<int64_t>(dim.begin(), dim.end()),
        keepdim, std::move(shapes));
    CacheNode(node);
  }

  auto result = torch_xla::bridge::AtenFromXlaTensor(
      torch_xla::XLATensor::Create(std::move(node), *common_device));
  return result;
};

LazyIr

class Amax : public XlaNode {
 public:
  static torch::lazy::OpKind ClassOpKind() {
    return torch::lazy::OpKind(at::aten::amax);
  }

  Amax(const torch::lazy::Value& self, const ::std::vector<int64_t>& dim,
       const bool& keepdim, std::vector<torch::lazy::Shape>&& shapes)
      : XlaNode(torch::lazy::OpKind(at::aten::amax), {self}, std::move(shapes),
                [&]() { return AmaxOutputShape(self, dim, keepdim); },
                /* num_outputs */ 1, torch::lazy::MHash(dim, keepdim)),
        dim(dim),
        keepdim(keepdim) {}

  std::string ToString() const override {
    std::stringstream ss;
    ss << XlaNode::ToString();
    ss << ", dim=" << dim;
    ss << ", keepdim=" << keepdim;
    return ss.str();
  }

  bool CanBeReused(const torch::lazy::Value& self,
                   const ::std::vector<int64_t>& dim,
                   const bool& keepdim) const {
    return false;
  }

  torch_xla::XlaOpVector Lower(LoweringContext* loctx) const override;

  ::std::vector<int64_t> dim;
  bool keepdim;
};

class Amin : public XlaNode {
 public:
  static torch::lazy::OpKind ClassOpKind() {
    return torch::lazy::OpKind(at::aten::amin);
  }

  Amin(const torch::lazy::Value& self, const ::std::vector<int64_t>& dim,
       const bool& keepdim, std::vector<torch::lazy::Shape>&& shapes)
      : XlaNode(torch::lazy::OpKind(at::aten::amin), {self}, std::move(shapes),
                [&]() { return AminOutputShape(self, dim, keepdim); },
                /* num_outputs */ 1, torch::lazy::MHash(dim, keepdim)),
        dim(dim),
        keepdim(keepdim) {}

  std::string ToString() const override {
    std::stringstream ss;
    ss << XlaNode::ToString();
    ss << ", dim=" << dim;
    ss << ", keepdim=" << keepdim;
    return ss.str();
  }

  bool CanBeReused(const torch::lazy::Value& self,
                   const ::std::vector<int64_t>& dim,
                   const bool& keepdim) const {
    return false;
  }

  torch_xla::XlaOpVector Lower(LoweringContext* loctx) const override;

  ::std::vector<int64_t> dim;
  bool keepdim;
};

@JackCaoG
Copy link
Collaborator Author

Oh I figured out what happened, it seems like codegen ops can't handle negative input, for example

t1 =torch.randn(4,3,4, device='xla:0')
t2 = torch.amax(t1, -2, True)

failed, but index -2 should be equivalent to index 1 here.

@JackCaoG
Copy link
Collaborator Author

This is blocked by pytorch/pytorch#82286

@JackCaoG JackCaoG changed the title [DRAFT] Codegen amin amax [BLOCKED] Codegen amin amax Jul 27, 2022
@miladm miladm added the BLOCKED label Jul 28, 2022
@JackCaoG JackCaoG changed the title [BLOCKED] Codegen amin amax Codegen amin amax Aug 10, 2022
@JackCaoG JackCaoG removed the BLOCKED label Aug 10, 2022
@JackCaoG
Copy link
Collaborator Author

@wonjoolee95 This one if ready for reviewing

Copy link
Collaborator

@wonjoolee95 wonjoolee95 left a comment

Choose a reason for hiding this comment

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

Nice! 🚀

@JackCaoG JackCaoG merged commit b8cef3e into master Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants