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

Host irs: LinearOp with pre-allocated output #3735

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions csrc/host_ir/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,35 @@ void HostIrEvaluator::handle(MatmulOp* matmul) {
}
}

void HostIrEvaluator::handle(LinearOp* linear) {
TensorView* in = linear->inA()->as<TensorView>();
TensorView* weight = linear->inB()->as<TensorView>();
TensorView* bias = linear->bias()->as<TensorView>();
TensorView* out = linear->out()->as<TensorView>();
NVF_ERROR(
expr_evaluator_.isKnown(in) && expr_evaluator_.isKnown(weight) &&
(!linear->has_bias() || expr_evaluator_.isKnown(bias)),
"Inputs of the Linear Op ",
linear->toString(),
"must be precomputed before being retrieved");

if (!expr_evaluator_.isKnown(out)) {
unhandled(linear);
return;
}

auto in_at = expr_evaluator_.evaluate(in).as<at::Tensor>();
auto weight_at = expr_evaluator_.evaluate(weight).as<at::Tensor>();
auto bias_at = expr_evaluator_.evaluate(bias).as<at::Tensor>();
auto out_at = expr_evaluator_.evaluate(out).as<at::Tensor>();

if (linear->has_bias()) {
at::linear_out(out_at, in_at, weight_at.squeeze(), bias_at.squeeze());
} else {
at::linear_out(out_at, in_at, weight_at.squeeze());
}
}

void HostIrEvaluator::handle(kir::Allocate* allocate) {
NVF_ERROR(
allocate->buffer()->isA<TensorView>(),
Expand Down
1 change: 1 addition & 0 deletions csrc/host_ir/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class HostIrEvaluator final : public OptOutDispatch {
void handle(EndCoalescing* end_coalescing) override;
void handle(kir::IfThenElse* if_then_else) override;
void handle(MatmulOp* matmul) override;
void handle(LinearOp* linear) override;
void handle(kir::Allocate* allocate) override;
void unhandled(Statement* stmt) override;

Expand Down
1 change: 0 additions & 1 deletion csrc/ir/internal_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,6 @@ class LinearOp : public Expr {
const ExpressionEvaluator& ee,
const std::vector<PolymorphicValue>& inputs) const override;

private:
bool has_bias() const {
return inputs().size() == 3;
}
Expand Down
86 changes: 86 additions & 0 deletions tests/cpp/test_host_irs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,92 @@ TEST_F(MatmulHostIrTest, HostIrMatmulOut) {
EXPECT_TRUE(ref_output.allclose(c_tensor));
}

using LinearHostIrTest = NVFuserTest;

TEST_F(LinearHostIrTest, HostIr) {
constexpr int64_t B = 32;
constexpr int64_t M = 64;
constexpr int64_t K = 128;
constexpr int64_t N = 256;

auto hic = std::make_unique<HostIrContainer>();
FusionGuard fg(hic.get());

TensorView* in = makeContigTensor(3);
TensorView* weight = makeContigTensor(2);
TensorView* bias = makeContigTensor(1);
TensorView* out = linear(in, weight, bias);

hic->addInput(in);
hic->addInput(weight);
hic->addInput(bias);
hic->addOutput(out);

hic->pushBackTopLevelExprs(out->definition());

HostIrEvaluator hie(std::move(hic));

auto options = at::TensorOptions().device(at::kCUDA, 0).dtype(torch::kFloat);
at::Tensor in_at = at::randn({B, M, K}, options);
at::Tensor weight_at = at::randn({N, K}, options);
at::Tensor bias_at = at::randn({N}, options);
std::unordered_map<Val*, c10::IValue> concrete_input_buffers = {
{hie.inputs().at(0), in_at},
{hie.inputs().at(1), weight_at},
{hie.inputs().at(2), bias_at}};

auto output = hie.runWithInput(concrete_input_buffers).at(0);

// validate
auto ref_output = at::linear(in_at, weight_at, bias_at);

EXPECT_TRUE(ref_output.allclose(output));
}

TEST_F(LinearHostIrTest, HostIrLinearOut) {
constexpr int64_t B = 32;
constexpr int64_t M = 64;
constexpr int64_t K = 128;
constexpr int64_t N = 256;

auto hic = std::make_unique<HostIrContainer>();
FusionGuard fg(hic.get());

TensorView* in = makeContigTensor(3);
TensorView* weight = makeContigTensor(2);
TensorView* bias = makeContigTensor(1);
TensorView* out = makeContigTensor(3);

auto linear_op = IrBuilder::create<LinearOp>(out, in, weight, bias);

hic->addInput(in);
hic->addInput(weight);
hic->addInput(bias);
hic->addInput(out);

hic->pushBackTopLevelExprs(linear_op);

HostIrEvaluator hie(std::move(hic));

auto options = at::TensorOptions().device(at::kCUDA, 0).dtype(torch::kFloat);
at::Tensor in_at = at::randn({B, M, K}, options);
at::Tensor weight_at = at::randn({N, K}, options);
at::Tensor bias_at = at::randn({N}, options);
at::Tensor out_at = at::empty({B, M, N}, options);
std::unordered_map<Val*, c10::IValue> concrete_input_buffers = {
{hie.inputs().at(0), in_at},
{hie.inputs().at(1), weight_at},
{hie.inputs().at(2), bias_at},
{hie.inputs().at(3), out_at}};

hie.runWithInput(concrete_input_buffers);

// validate
auto ref_output = at::linear(in_at, weight_at, bias_at);

EXPECT_TRUE(ref_output.allclose(out_at));
}

using SelectHostIrTestParams = bool;
using SelectHostIrTest = NVFuserFixtureParamTest<SelectHostIrTestParams>;

Expand Down
Loading