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

Add Python bindings for override mechanism. #1526

Merged
merged 3 commits into from
Dec 9, 2024

Conversation

vcanicTT
Copy link
Contributor

@vcanicTT vcanicTT commented Dec 6, 2024

Add initial version of Python lib for optimizer overrides. This library is just pybind wrapper around C++ class for optimizer overrides in MLIR.

void populatePassesModule(py::module &m);

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit Extra line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

private:
// Options for the TTIR to TTNN backend pipeline,
// we use them to extract the names and the deafulat values.
TTIRToTTNNBackendPipelineOptions pipelineOptions;
// TTIRToTTNNBackendPipelineOptions pipelineOptions;
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. Done.

@@ -13,6 +13,19 @@

namespace mlir::tt::ttnn {

struct OptionNames {

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit Extra line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -13,6 +13,19 @@

namespace mlir::tt::ttnn {

struct OptionNames {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you switch to this helper struct for names? Names are already defined in pipeline options, previous solution was thus better it didn't require redefining in multiple places in code when adding new option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't bind LLVM structures (class) and their virtual methods. LLVM has separate RTTI. Done.

@@ -6,6 +6,18 @@

namespace mlir::tt::ttnn {

const std::string OptionNames::optimizerPassEnabled = "enable-optimizer";
Copy link
Contributor

Choose a reason for hiding this comment

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

See my other comment above for these new constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

const std::string OptionNames::overrideOutputLayout = "override-output-layout";
const std::string OptionNames::memoryLayoutAnalysisEnabled =
"memory-layout-analysis-enabled";
const std::string OptionNames::memReconfigEnabled = "memreconfig-enabled";
Copy link
Contributor

Choose a reason for hiding this comment

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

Constexpr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@odjuricicTT odjuricicTT left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -65,10 +68,22 @@ class OptimizerOverridesHandler {
TensorMemoryLayout, tt::ttnn::Layout,
tt::DataType);

// Wrapper methods we use to expose the adders to the python bindings
std::unordered_map<std::string, InputLayoutOverrideParams>
getInputLayoutOverridesPybindWrapper() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason to create wrappers instead of changing the existing methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

C++ maps bind to Python dict, it's easier to bind and deal with C++ types instead of LLVM. I use wrappers only for bindings, within the MLIR C++ you can use both of them.

@@ -6,6 +6,18 @@

namespace mlir::tt::ttnn {

const std::string OptionNames::optimizerPassEnabled = "enable-optimizer";
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing that there is a problem when reading these from pipeline options?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. We can't bind class TTIRToTTNNBackendPipelineOptions, we build python lib with RTTI, and if the structure has or inherits some virtual methods, something wrong happens with the linker. We can talk more about this with Stefi and Topalko, about what we can do in the future, but I think we can't do too much. If we want to expose something from MLIR to Python in this way, we always need to have some simple(C++) structure, or to extend pybind and LLVM. There is also, llvmpy lib, but often they have the same issues with the linking.

@@ -6,6 +6,18 @@

namespace mlir::tt::ttnn {

const std::string OptionNames::optimizerPassEnabled = "enable-optimizer";
const std::string OptionNames::overrideInputLayout = "insert-memreconfig";
const std::string OptionNames::overrideOutputLayout = "override-output-layout";
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Might be cleaner just to have these set in the .h file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@vprajapati-tt vprajapati-tt left a comment

Choose a reason for hiding this comment

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

Bindings LGTM, glad to see you got around the issues :) Unsure if we want to hold both overrides and optimizer_overrides, there isn't anything planned for the overrides module at the moment.

@vcanicTT
Copy link
Contributor Author

vcanicTT commented Dec 8, 2024

Bindings LGTM, glad to see you got around the issues :) Unsure if we want to hold both overrides and optimizer_overrides, there isn't anything planned for the overrides module at the moment.

I'm not sure about that module, you can remove it.

Optimizer teams want to have separate module for all kinds of overrides we use through the optimizer. If we want to extend override mechanism, we need to design it better and write documentation before we start the implementation.

@vcanicTT vcanicTT force-pushed the vcanic/override_mechanism_pybind branch from a95db1d to 458f00a Compare December 8, 2024 09:31
Copy link
Contributor

@nobradovictt nobradovictt left a comment

Choose a reason for hiding this comment

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

Thanks for addressing feedback promptly! Enjoy your vacation!

@vcanicTT vcanicTT merged commit 223d244 into main Dec 9, 2024
19 checks passed
azecevicTT pushed a commit that referenced this pull request Dec 17, 2024
* Add Python bindings for override mechanism.

* Add Python bindings for override mechanism.

* Add Python bindings for override mechanism.
@mtopalovicTT mtopalovicTT deleted the vcanic/override_mechanism_pybind branch December 31, 2024 08:45
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.

4 participants