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

Triton #6798

Merged
merged 67 commits into from
Jun 7, 2024
Merged

Triton #6798

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
6dccf0a
Update infra_triggers.tf
ManfeiBai Oct 4, 2023
9828123
Skeleton trition support
bhavya01 Mar 20, 2024
99bf48d
Merge branch 'master' into triton
bhavya01 Mar 20, 2024
b89e558
Fix bugs
bhavya01 Mar 21, 2024
64189bd
Fix custom call invocation
bhavya01 Mar 21, 2024
0c208ef
Refactor to include gpu custom call and create triton dir
bhavya01 Mar 22, 2024
b553ba7
Lint fixes
bhavya01 Mar 22, 2024
c5129e6
python lint fix
bhavya01 Mar 22, 2024
48e7127
Updated base image for CI
bhavya01 Mar 27, 2024
e04fc97
Update github workflow gcr image
bhavya01 Mar 28, 2024
37bf127
Merge branch 'master' into custom
bhavya01 Mar 28, 2024
6061895
Remove xrt build and test file
bhavya01 Mar 28, 2024
f59ddbf
Add temporary test to run triton kernel
bhavya01 Mar 28, 2024
158aed4
Fix tests
bhavya01 Mar 28, 2024
87b92c5
Update payload for xla gpu custom call
bhavya01 Mar 29, 2024
847ccc5
Update gpu runner
bhavya01 Mar 29, 2024
eca6d52
Merge branch 'master' into triton
bhavya01 Apr 4, 2024
2348ca3
Extract payload from triton kernel programatically
bhavya01 Apr 12, 2024
110c8c6
Merge branch 'master' into triton
bhavya01 Apr 12, 2024
a226150
Lint fixes
bhavya01 Apr 12, 2024
4c1f4f5
Only build triton files for GPU
bhavya01 Apr 12, 2024
431f822
build pytorch for ampere gpus
bhavya01 Apr 13, 2024
4bade16
c++ lint fix
bhavya01 Apr 13, 2024
1c5b47d
Python lint fix
bhavya01 Apr 13, 2024
3138a92
Fix torch cuda arch list
bhavya01 Apr 13, 2024
3f00cfd
Use a bigger machine for CI build
bhavya01 Apr 13, 2024
e729cfb
Add triton test to run_tests.sh
bhavya01 Apr 13, 2024
8e304c0
Update triton env variable
bhavya01 Apr 15, 2024
27bdc3a
Set up a separate CI for triton tests
bhavya01 Apr 15, 2024
9a3ef84
Fix github workflow to add _triton.yml
bhavya01 Apr 15, 2024
ade444d
Rebuild torch xla for triton tests
bhavya01 Apr 15, 2024
cb0bb85
Create a separate CI tab for triton tests
bhavya01 Apr 16, 2024
015b1ad
Separate build and test phase for triton
bhavya01 Apr 16, 2024
a18028a
Fix flags for docker run container
bhavya01 Apr 16, 2024
993ee92
Update triton.yml to output docker image
bhavya01 Apr 16, 2024
a87b782
Add a python binding to register custom calls and remove jax files
bhavya01 May 10, 2024
bf05d1b
Fix lint
bhavya01 May 10, 2024
4582fe8
Merge main
bhavya01 May 10, 2024
9680167
Merge master
bhavya01 May 10, 2024
a7b94c6
Merge master after updating
bhavya01 May 10, 2024
e14636a
Update CI to use cuda plugin
bhavya01 May 10, 2024
256d819
Install jaxlib while setting up triton tests
bhavya01 May 10, 2024
c616e64
Install triton package while running triton tests
bhavya01 May 10, 2024
60b8d18
Experimental: Build pytorch with cuda
bhavya01 May 13, 2024
2bde624
Revert build pytorch with CUDA
bhavya01 May 14, 2024
e6c4e0a
Merge branch 'master' into triton
bhavya01 May 14, 2024
14ee545
Remove ansible path for triton CI
bhavya01 May 14, 2024
25acb26
Style fixes
bhavya01 May 20, 2024
6b0ac18
[Experimental] test new CI
bhavya01 May 28, 2024
4d97150
[Experimental] Set XLA_CUDA=0 for cuda arch in ansible
bhavya01 May 28, 2024
e079049
[Experimental] Update CI to build pytorch cuda with ansible
bhavya01 May 29, 2024
d9c89b6
Update CI
bhavya01 May 30, 2024
7a6c809
Fix CI workflow file
bhavya01 May 30, 2024
6b1954d
Fix CI workflow
bhavya01 May 30, 2024
21797a6
Fix the wheels installed for tests requiring torch cuda
bhavya01 May 30, 2024
e6e89d3
Add compute_capability=8.6 for xla cuda plugin
bhavya01 May 31, 2024
ac45fe1
update TORCH_CUDA_ARCH_LIST
bhavya01 May 31, 2024
f828fbb
Experimental build torch and torch_xla cuda wheels
bhavya01 May 31, 2024
ac56c00
Merge branch 'master' into triton
bhavya01 May 31, 2024
c3b8653
Update build_and_test.yml
bhavya01 May 31, 2024
a1168c6
Update dlpack test to only use one device
bhavya01 May 31, 2024
39551a2
Remove compute capability 8.6 from cuda plugin
bhavya01 May 31, 2024
35e0869
Remove triton.sh
bhavya01 May 31, 2024
f95d898
Default empty torch_cuda_arch_list in ansible config
bhavya01 May 31, 2024
291104d
Merge branch 'master' into triton
bhavya01 Jun 5, 2024
f5c9b1a
Revert CI changes
bhavya01 Jun 6, 2024
5b23969
Revert CI changes pt2
bhavya01 Jun 6, 2024
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
6 changes: 3 additions & 3 deletions .github/workflows/_build_torch_with_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ on:
type: string
description: Base image for builds
torch-commit:
required: true
type: string
description: torch-commit
required: true
type: string
description: torch-commit
runner:
required: false
type: string
Expand Down
68 changes: 68 additions & 0 deletions test/test_triton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
import torch
from torch import nn as nn
import unittest

import torch_xla.experimental.triton as xla_triton
import torch_xla
from torch_xla import runtime as xr

import triton
import triton.language as tl


@triton.jit
def add_kernel(
x_ptr, # *Pointer* to first input vector.
y_ptr, # *Pointer* to second input vector.
output_ptr, # *Pointer* to output vector.
n_elements, # Size of the vector.
BLOCK_SIZE: tl.constexpr, # Number of elements each program should process.
# NOTE: `constexpr` so it can be used as a shape value.
):
# Triton add kernel from https://github.com/openai/triton/blob/main/python/tutorials/01-vector-add.py#L28
# There are multiple 'programs' processing different data. We identify which program
# we are here:
pid = tl.program_id(axis=0) # We use a 1D launch grid so axis is 0.
# This program will process inputs that are offset from the initial data.
# For instance, if you had a vector of length 256 and block_size of 64, the programs
# would each access the elements [0:64, 64:128, 128:192, 192:256].
# Note that offsets is a list of pointers:
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
# Create a mask to guard memory operations against out-of-bounds accesses.
mask = offsets < n_elements
# Load x and y from DRAM, masking out any extra elements in case the input is not a
# multiple of the block size.
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
output = x + y
# Write x + y back to DRAM.
tl.store(output_ptr + offsets, output, mask=mask)


class TritonTest(unittest.TestCase):

@unittest.skipIf(xr.device_type() != 'CUDA', "This test only works on GPU.")
def test_gpu_custom_call_triton_add(self):
size = 16

x = torch.arange(size, dtype=torch.int64).to("xla")
y = torch.arange(size, dtype=torch.int64).to("xla")
output = torch.empty_like(x)
block_size = 8
grid = (triton.cdiv(size, block_size),)
payload = xla_triton.triton_call(
x, y, output, size, kernel=add_kernel, grid=grid, BLOCK_SIZE=block_size)
output = torch_xla._XLAC._xla_gpu_custom_call([x, y], payload,
[output.shape], [torch.int64])
output_torch = x + y
self.assertTrue(torch.allclose(output[0].cpu(), output_torch.cpu()))


if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
torch.set_default_dtype(torch.float32)
torch.manual_seed(42)
test = unittest.main()
sys.exit(0 if test.result.wasSuccessful() else 1)
1 change: 1 addition & 0 deletions torch_xla/csrc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ ptxla_cc_library(
"@xla//xla/service:hlo_verifier",
"@xla//xla/service:sharding_propagation",
"@xla//xla/service/spmd:spmd_partitioner",
"@xla//xla/service:custom_call_target_registry",
],
)

Expand Down
45 changes: 35 additions & 10 deletions torch_xla/csrc/init_python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "tsl/profiler/lib/traceme.h"
#include "xla/pjrt/distributed/distributed.h"
#include "xla/python/profiler/internal/traceme_wrapper.h"
#include "xla/service/custom_call_target_registry.h"
#include "xla/service/hlo_parser.h"

namespace torch_xla {
Expand Down Expand Up @@ -202,6 +203,24 @@ std::vector<std::vector<int64_t>> CreateReduceGroups(const py::list& groups) {
return replica_groups;
}

std::vector<at::Tensor> XlaCustomCall(
const std::vector<at::Tensor>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
const std::vector<py::object>& output_dtypes, bool is_tpu) {
std::vector<at::ScalarType> dtypes;
dtypes.reserve(output_dtypes.size());
for (auto& dtype : output_dtypes) {
dtypes.push_back(reinterpret_cast<THPDtype*>(dtype.ptr())->scalar_type);
}

if (is_tpu) {
return bridge::AtenFromXlaTensors(tensor_methods::tpu_custom_call(
bridge::GetXlaTensors(inputs), payload, output_shapes, dtypes));
}
return bridge::AtenFromXlaTensors(tensor_methods::gpu_custom_call(
bridge::GetXlaTensors(inputs), payload, output_shapes, dtypes));
}

std::vector<std::pair<int64_t, int64_t>> CreateSourceTargetPairs(
const py::list& pairs) {
std::vector<std::pair<int64_t, int64_t>> source_target_pairs;
Expand Down Expand Up @@ -2401,16 +2420,22 @@ void InitXlaModuleBindings(py::module m) {
const std::vector<std::vector<int64_t>>& output_shapes,
const std::vector<py::object>& output_dtypes)
-> std::vector<at::Tensor> {
std::vector<at::ScalarType> dtypes;
dtypes.reserve(output_dtypes.size());
for (auto& dtype : output_dtypes) {
dtypes.push_back(
reinterpret_cast<THPDtype*>(dtype.ptr())->scalar_type);
}

auto xtensors = tensor_methods::tpu_custom_call(
bridge::GetXlaTensors(inputs), payload, output_shapes, dtypes);
return bridge::AtenFromXlaTensors(xtensors);
return XlaCustomCall(inputs, payload, output_shapes, output_dtypes,
/*is_tpu=*/true);
});
m.def("_xla_gpu_custom_call",
[](const std::vector<at::Tensor>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
const std::vector<py::object>& output_dtypes)
-> std::vector<at::Tensor> {
return XlaCustomCall(inputs, payload, output_shapes, output_dtypes,
/*is_tpu=*/false);
});
m.def("_xla_register_custom_call_target",
[](const std::string& fn_name, const py::capsule& function_ptr,
const std::string& platform) {
XLA_REGISTER_CUSTOM_CALL_TARGET_WITH_SYM(
fn_name, function_ptr.get_pointer(), platform);
});
m.def("_set_xla_custom_op_name_prefix",
[](const at::Tensor& input, const std::string& op_name_prefix,
Expand Down
37 changes: 37 additions & 0 deletions torch_xla/csrc/ops/gpu_custom_call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "torch_xla/csrc/ops/gpu_custom_call.h"

#include "torch_xla/csrc/lowering_context.h"
#include "torch_xla/csrc/ops/xla_ops.h"
#include "torch_xla/csrc/xla_lower_util.h"

namespace torch_xla {

GpuCustomCall::GpuCustomCall(torch::lazy::OpList inputs,
xla::Shape output_shape,
const std::string& payload)
: XlaNode(xla_gpu_custom_call, inputs, std::move(output_shape),
/*num_outputs=*/output_shape.tuple_shapes_size(),
torch::lazy::MHash(payload)),
payload_(payload) {}

torch::lazy::NodePtr GpuCustomCall::Clone(torch::lazy::OpList operands) const {
return torch::lazy::MakeNode<GpuCustomCall>(operands, xla_shape(), payload_);
}

XlaOpVector GpuCustomCall::Lower(LoweringContext* loctx) const {
std::vector<xla::XlaOp> inputs;
inputs.reserve(operands().size());
for (auto& operand : operands()) {
inputs.push_back(loctx->GetOutputOp(operand));
}
auto output = BuildGpuCustomCall(inputs, xla_shape(), payload_);
return ReturnOps(output, loctx);
}

std::string GpuCustomCall::ToString() const {
std::stringstream ss;
ss << XlaNode::ToString() << ", " << payload_;
return ss.str();
}

} // namespace torch_xla
25 changes: 25 additions & 0 deletions torch_xla/csrc/ops/gpu_custom_call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef XLA_TORCH_XLA_CSRC_OPS_GPU_CUSTOM_CALL_H_
#define XLA_TORCH_XLA_CSRC_OPS_GPU_CUSTOM_CALL_H_

#include "torch_xla/csrc/ir.h"

namespace torch_xla {
class GpuCustomCall : public XlaNode {
public:
// Make a GPU custom call with payload, e.g., Triton.
GpuCustomCall(torch::lazy::OpList inputs, xla::Shape output_shape,
const std::string& payload);

torch::lazy::NodePtr Clone(torch::lazy::OpList operands) const override;

XlaOpVector Lower(LoweringContext* loctx) const override;

std::string ToString() const override;

private:
std::string payload_;
};

} // namespace torch_xla

#endif // XLA_TORCH_XLA_CSRC_OPS_GPU_CUSTOM_CALL_H_
1 change: 1 addition & 0 deletions torch_xla/csrc/ops/xla_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ const OpKindWrapper xla_unselect("xla::unselect");
const OpKindWrapper xla_update_slice("xla::update_slice");
const OpKindWrapper xla_custom_sharding("xla::custom_sharding");
const OpKindWrapper xla_tpu_custom_call("xla::tpu_custom_call");
const OpKindWrapper xla_gpu_custom_call("xla::gpu_custom_call");

} // namespace torch_xla
1 change: 1 addition & 0 deletions torch_xla/csrc/ops/xla_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern const OpKindWrapper xla_unselect;
extern const OpKindWrapper xla_update_slice;
extern const OpKindWrapper xla_custom_sharding;
extern const OpKindWrapper xla_tpu_custom_call;
extern const OpKindWrapper xla_gpu_custom_call;

} // namespace torch_xla

Expand Down
34 changes: 34 additions & 0 deletions torch_xla/csrc/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "torch_xla/csrc/ops/generic.h"
#include "torch_xla/csrc/ops/generic_slice.h"
#include "torch_xla/csrc/ops/get_dimensions_size.h"
#include "torch_xla/csrc/ops/gpu_custom_call.h"
#include "torch_xla/csrc/ops/hardtanh_backward.h"
#include "torch_xla/csrc/ops/index_ops.h"
#include "torch_xla/csrc/ops/index_select.h"
Expand Down Expand Up @@ -566,6 +567,39 @@ void custom_sharding_(
input->SetShardingSpec(*sharding_spec);
}

std::vector<XLATensorPtr> gpu_custom_call(
const std::vector<XLATensorPtr>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
const std::vector<at::ScalarType>& output_dtypes) {
XLA_CHECK(inputs.size() > 0) << "inputs are empty";

std::vector<torch::lazy::Value> values;
values.reserve(inputs.size());
for (const auto& input : inputs) {
values.push_back(input->GetIrValue());
}

XLA_CHECK_EQ(output_shapes.size(), output_dtypes.size());
std::vector<xla::Shape> output_xla_shapes;
output_xla_shapes.reserve(output_shapes.size());
for (size_t i = 0; i < output_shapes.size(); ++i) {
output_xla_shapes.push_back(xla::ShapeUtil::MakeShape(
MakeXlaPrimitiveType(output_dtypes[i], &(inputs[0]->GetDevice())),
output_shapes[i]));
}

auto node = torch::lazy::MakeNode<GpuCustomCall>(
values, xla::ShapeUtil::MakeTupleShape(output_xla_shapes), payload);

std::vector<XLATensorPtr> outputs;
outputs.reserve(output_shapes.size());
for (size_t i = 0; i < output_shapes.size(); ++i) {
outputs.push_back(
inputs[0]->CreateFrom(torch::lazy::Value(node, i), output_dtypes[i]));
}
return outputs;
}

std::vector<XLATensorPtr> tpu_custom_call(
const std::vector<XLATensorPtr>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
Expand Down
5 changes: 5 additions & 0 deletions torch_xla/csrc/tensor_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ void custom_sharding_(
const std::shared_ptr<XLATensor::ShardingSpec>& spec,
const CustomSharding::Type& type = CustomSharding::Type::kSharding);

std::vector<XLATensorPtr> gpu_custom_call(
const std::vector<XLATensorPtr>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
const std::vector<at::ScalarType>& output_dtypes);

std::vector<XLATensorPtr> tpu_custom_call(
const std::vector<XLATensorPtr>& inputs, const std::string& payload,
const std::vector<std::vector<int64_t>>& output_shapes,
Expand Down
26 changes: 25 additions & 1 deletion torch_xla/csrc/xla_lower_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,11 +1272,35 @@ xla::XlaOp BuildCustomSharding(const xla::XlaOp& input, const std::string& type,
output_shape);
}

std::vector<xla::XlaOp> BuildGpuCustomCall(
const std::vector<xla::XlaOp>& inputs, const xla::Shape& output_shape,
const std::string& payload) {
std::vector<xla::Shape> input_shapes;
input_shapes.reserve(inputs.size());
for (const auto& input : inputs) {
input_shapes.push_back(ShapeHelper::ShapeOfXlaOp(input));
}

XLA_CHECK(inputs.size() > 0) << "inputs are empty";
xla::XlaOp outputs = xla::CustomCallWithLayout(
inputs[0].builder(),
/*call_target_name=*/"triton_kernel_call", inputs, output_shape,
input_shapes, payload, false, {}, nullptr,
xla::CustomCallSchedule::SCHEDULE_NONE,
xla::CustomCallApiVersion::API_VERSION_STATUS_RETURNING);
std::vector<xla::XlaOp> result;
int num_outputs = output_shape.tuple_shapes_size();
result.reserve(num_outputs);
for (int i = 0; i < num_outputs; ++i) {
result.push_back(xla::GetTupleElement(outputs, i));
}
return result;
}

std::vector<xla::XlaOp> BuildTpuCustomCall(
const std::vector<xla::XlaOp>& inputs, const xla::Shape& output_shape,
const std::string& payload) {
XLA_CHECK(output_shape.IsTuple()) << "output_shape is not a tuple";

// We need to enforce the default C-order (major-to-minor) layouts for inputs
// to Mosaic and outputs from Mosaic.
std::vector<xla::Shape> input_shapes;
Expand Down
4 changes: 4 additions & 0 deletions torch_xla/csrc/xla_lower_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ std::vector<xla::XlaOp> BuildTpuCustomCall(
xla::XlaOp BuildNms(xla::XlaOp boxes, xla::XlaOp scores,
xla::XlaOp iou_threshold);

std::vector<xla::XlaOp> BuildGpuCustomCall(
const std::vector<xla::XlaOp>& inputs, const xla::Shape& output_shape,
const std::string& payload);

} // namespace torch_xla

#endif // XLA_TORCH_XLA_CSRC_XLA_LOWER_UTIL_H_
Loading
Loading