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

[Autogen Phi Operants & Prim-Relevant] Autogen phi tensor operants, operants_base, operants_manager #50582

Merged
merged 7 commits into from Feb 17, 2023

Conversation

jiahy0825
Copy link
Contributor

@jiahy0825 jiahy0825 commented Feb 16, 2023

PR types

New features

PR changes

Others

Describe

Autogen phi tensor operants, operants_base, operants_manager. Example codes:

tensor_operants.h

// Generated by paddle/phi/api/yaml/generator/tensor_gen.py

#pragma once

#include "paddle/phi/api/include/operants_base.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/core/macros.h"


namespace paddle {

namespace operants {

class PhiTensorOperants : public TensorOperantsBase {
 private:
  DISABLE_COPY_AND_ASSIGN(PhiTensorOperants);

 public:
  PhiTensorOperants() = default;


  Tensor add(const Tensor& x, const Tensor& y);
};

}  // namespace operants
}  // namespace paddle

tensor_operants.cc

// Generated by paddle/phi/api/yaml/generator/tensor_gen.py

#include "paddle/phi/api/include/tensor_operants.h"

#include "paddle/phi/api/include/api.h"


namespace paddle {

namespace operants {


Tensor PhiTensorOperants::add(const Tensor& x, const Tensor& y) {
  return paddle::experimental::add(x, y);
}

}  // namespace operants
}  // namespace paddle

operants_base.h

// Generated by paddle/phi/api/yaml/generator/tensor_gen.py

#pragma once

#include "paddle/phi/api/include/tensor.h"


namespace paddle {

namespace operants {

using Tensor = paddle::experimental::Tensor;

class TensorOperantsBase {
 public:
  virtual ~TensorOperantsBase() = default;

  virtual Tensor add(const Tensor& x, const Tensor& y) = 0;
};

}  // namespace operants
}  // namespace paddle

operants_manager.h

// Generated by paddle/phi/api/yaml/generator/tensor_gen.py

#pragma once

#include "paddle/phi/api/include/operants_base.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/core/macros.h"


namespace paddle {

using Tensor = paddle::experimental::Tensor;
using TensorOperantsBase = paddle::operants::TensorOperantsBase;

class OperantsManager {
 private:
  OperantsManager() = default;
  DISABLE_COPY_AND_ASSIGN(OperantsManager);

 public:
  std::unique_ptr<TensorOperantsBase> eager_operants{nullptr};
  std::unique_ptr<TensorOperantsBase> static_operants{nullptr};
  std::unique_ptr<TensorOperantsBase> phi_operants{nullptr};

 public:
  static OperantsManager& Instance();


  Tensor add(const Tensor& x, const Tensor& y);
};

}  // namespace paddle

operants_manager.cc

// Generated by paddle/phi/api/yaml/generator/tensor_gen.py

#include "paddle/phi/api/include/operants_manager.h"

#include "gflags/gflags.h"
#include "glog/logging.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"


DECLARE_string(tensor_operants_mode);

namespace paddle {

OperantsManager& OperantsManager::Instance() {
  static OperantsManager g_op_manager;
  return g_op_manager;
}


Tensor OperantsManager::add(const Tensor& x, const Tensor& y) {
  if (FLAGS_tensor_operants_mode == "eager") {
    PADDLE_ENFORCE_NE(
        this->eager_operants.get(),
        nullptr,
        phi::errors::Unavailable("The eager_operants pointer of "
                                 "OperantsManager is not initialized"));
    VLOG(4) << "OperantsManager reaches eager mode";
    return this->eager_operants->add(x, y);
  } else if (FLAGS_tensor_operants_mode == "static") {
    PADDLE_ENFORCE_NE(
        this->static_operants.get(),
        nullptr,
        phi::errors::Unavailable("The static_operants pointer of "
                                 "OperantsManager is not initialized"));
    VLOG(4) << "OperantsManager reaches static mode";
    return this->static_operants->add(x, y);
  } else if (FLAGS_tensor_operants_mode == "phi") {
    PADDLE_ENFORCE_NE(
        this->phi_operants.get(),
        nullptr,
        phi::errors::Unavailable(
            "The phi_operants pointer of OperantsManager is not initialized"));
    VLOG(4) << "OperantsManager reaches phi mode";
    return this->phi_operants->add(x, y);
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "FLAGS_tensor_operants_mode is not nitialized, please set "
        "FLAGS_tensor_operants_mode first, which currently supports eager, "
        "phi, and static mode"));
  }
}

}  // namespace paddle

@paddle-bot
Copy link

paddle-bot bot commented Feb 16, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

# tensor and tensor operants file
set(tensor_gen_file
${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_gen.py)
set(api_prim_yaml_file ${CMAKE_SOURCE_DIR}/paddle/fluid/prim/api/api.yaml)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -0,0 +1,454 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

tensor_gen名字看上去像是生成tensor的,文件名可能需要换一个

Copy link
Contributor Author

Choose a reason for hiding this comment

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

后续本文件也会用来生成 Tensor

This file will be used to generate Tensor in the near future.

@jiahy0825 jiahy0825 merged commit 5094aa0 into PaddlePaddle:develop Feb 17, 2023
@jiahy0825 jiahy0825 changed the title [Autogen Phi Operants] Autogen phi tensor operants, operants_base, operants_manager [Autogen Phi Operants & Prim-Relevant] Autogen phi tensor operants, operants_base, operants_manager Feb 23, 2023
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.

None yet

3 participants