Skip to content

Commit

Permalink
[Bug] Solves issue when certain inputs/constants aren't properly decl…
Browse files Browse the repository at this point in the history
…ared during MLIR emit

Previously, MLIR emit was hiting edge cases when declaring constant inputs. More precisely,
they were mostly skipped. This fix redefines how inputs are recognized (using kInput node type),
and properly distinguish regular and constant inputs vs model parameters.

Issue uncovered during #112 op bringup (reciprocal). At the same time, PR related to #112 is
testing this case.

Fixes #201
  • Loading branch information
nvukobratTT committed Aug 30, 2024
1 parent c5be9c6 commit 87a0bc3
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pybuda/csrc/passes/lower_to_mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "lower_to_mlir.hpp"

// Standard headers
#include <iostream>
#include <cstdint>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -132,6 +133,8 @@ class MLIRGenerator
throw std::runtime_error("Variable " + node->name() + " already declared in the current scope.");
}

log_trace(LogMLIRCompiler, "Declaring {} in the current scope.", node->name());

symbolTable_[node->name()] = {value, node};
}

Expand Down Expand Up @@ -171,8 +174,18 @@ class MLIRGenerator
symbolTable_.clear();

// Add the graph inputs to the argument list
for (auto *input: graph->ordered_module_inputs()) //for (auto *input : graph->nodes_by_type(tt::graphlib::kInput))
for (auto *input : graph->nodes_by_type(tt::graphlib::kInput))
{
log_trace(LogMLIRCompiler, "Adding input {} to the argument list.", input->name());

// Check whether the input is actually an parameter of the graph. If so, skip it.
graphlib::InputNode *input_node = dynamic_cast<graphlib::InputNode *>(input);
if (input_node->is_parameter())
{
log_trace(LogMLIRCompiler, "Skipping input {} as it is a parameter.", input->name());
continue;
}

argument_nodes.push_back(input);
argument_types.push_back(get_node_type(input));
}
Expand All @@ -185,8 +198,10 @@ class MLIRGenerator
// for forward and backward subgraphs (via GraphTraversalContext).
if (graph->data_users(parameter).empty())
{
log_trace(LogMLIRCompiler, "Skipping parameter {} as it is not used in the current graph context.", parameter->name());
continue;
}
log_trace(LogMLIRCompiler, "Adding parameter {} to the argument list.", parameter->name());

argument_nodes.push_back(parameter);
argument_types.push_back(get_node_type(parameter));
Expand All @@ -201,6 +216,7 @@ class MLIRGenerator

for (auto *output : output_nodes)
{
log_trace(LogMLIRCompiler, "Adding output {} to the return list.", output->name());
returns.push_back(get_node_type(output));
}

Expand All @@ -215,6 +231,7 @@ class MLIRGenerator
llvm::SmallVector<mlir::NamedAttribute, 1> named_attributes;
named_attributes.push_back(builder_.getNamedAttr("ttir.name", builder_.getStringAttr(argument_node->name())));
func.setArgAttrs(i, named_attributes);
log_trace(LogMLIRCompiler, "Set argument name {} for function argument {}.", argument_node->name(), i);
}

// Start the body of the function by creating an entry block.
Expand All @@ -241,9 +258,9 @@ class MLIRGenerator
// Skip if the node isn't TTForge operation
if (node->node_type() != tt::graphlib::NodeType::kPyOp)
{
log_trace(LogMLIRCompiler, "Skipping node {} as it is not a TTForge operation.", node->name());
continue;
}

log_trace(LogMLIRCompiler, "Emitting MLIR for node {}", node->name());

tt::graphlib::OpNode *op_node = node->as<tt::graphlib::OpNode>();
Expand Down Expand Up @@ -353,9 +370,18 @@ class MLIRGenerator
{
llvm::SmallVector<mlir::Value> operands;

#ifdef DEBUG
// Log all values from symbolTable_
log_trace(LogMLIRCompiler, "Logging all keys from symbolTable_");
for (const auto& entry : symbolTable_)
{
log_trace(LogMLIRCompiler, "Key: {}", entry.first);
}
#endif

for (auto operand : graph->data_operands(op_node))
{
TT_ASSERT(symbolTable_.find(operand->name()) != symbolTable_.end(), "Operand " + operand->name() + "not found in symbol table.");
TT_ASSERT(symbolTable_.find(operand->name()) != symbolTable_.end(), "Operand " + operand->name() + " not found in symbol table.");
operands.push_back(symbolTable_.at(operand->name()).first);
}

Expand Down

0 comments on commit 87a0bc3

Please sign in to comment.