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

Integrate Java Triton example with Intel Triton Backend #241

Open
wants to merge 15 commits into
base: code-reflection
Choose a base branch
from

Conversation

hanklo6
Copy link

@hanklo6 hanklo6 commented Sep 25, 2024

Babylon Java Triton example translates Java source code with Java Triton API into code model by code reflection.

In this PR, we traverse the given code model and output Triton MLIR dialect in the generic form, and then inject generated MLIR dialect into the Intel Triton backend. We then utilize Intel Triton backend to compile the Triton MLIR dialect into a SPIR-V module. Use Jextract to create Java binding of Intel Level Zero runtime and launch the given kernel function with it on Intel GPUs.

Usage

Navigate to the cr-example/triton directory and execute mvn clean test. This will generate multiple MLIR files in the result directory ready to be processed by the Triton backend.

Next, modify the compiler.py file within the intel-xpu-triton-backend project by applying the patch git apply add-mlir-insertion.patch. Then run the Triton backend by running python3 translate.py.

The Triton backend will generate SPIR-V files, which will be located under ~/.triton/cache/{hash_value}/{kernel_name}/{kernel_name}.spv.

To create a binding for Level Zero, execute the below commands:

$JEXTRACT_DIR/bin/jextract --output src/gen/java -I /usr/include -t oneapi.levelzero level-zero/include/ze_api.h
$JAVA_HOME/bin/javac -cp target/classes -d target/classes src/gen/java/oneapi/levelzero/*.java
$JAVA_HOME/bin/jar cf levelzero.jar -C target/classes/ .

The will generate levelzero.jar in the current directory.

After getting JAR files for Level Zero and JSON-java, proceed to compile and run the launcher LevelZero.java with the following commands:

babylon/build/linux-x86_64-server-release/jdk/bin/javac -cp .:levelzero.jar:json-java.jar LevelZero.java
babylon/build/linux-x86_64-server-release/jdk/bin/java -ea -cp .:levelzero.jar:json-java.jar LevelZero

Ensure the hash values in~/.triton/cache match those used in the LevelZero.java.

Dependencies


Progress

  • Change must not contain extraneous whitespace

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/241/head:pull/241
$ git checkout pull/241

Update a local copy of the PR:
$ git checkout pull/241
$ git pull https://git.openjdk.org/babylon.git pull/241/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 241

View PR using the GUI difftool:
$ git pr show -t 241

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/241.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 25, 2024

👋 Welcome back hanklo6! A progress list of the required criteria for merging this PR into code-reflection will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 25, 2024

@hanklo6 This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

Integrate Java Triton example with Intel Triton Backend

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 1203 new commits pushed to the code-reflection branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@PaulSandoz) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@hanklo6 hanklo6 changed the title Enhance Triton MLIR for Triton Backend Integrate Java Triton example with Intel Triton Backend Sep 25, 2024
@hanklo6 hanklo6 marked this pull request as ready for review October 9, 2024 21:38
@openjdk openjdk bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 9, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 9, 2024

Webrevs

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 31, 2024

@hanklo6 This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@PaulSandoz
Copy link
Member

Keep alive

Copy link
Member

@PaulSandoz PaulSandoz left a comment

Choose a reason for hiding this comment

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

Thank you for this effort. It's not easy to hook this up and you did it without any help!

Initially i have just focused on the code in the triton package.

Comment on lines +469 to +471
int stride_am, @Constant int stride_ak,
int stride_bk, @Constant int stride_bn,
int stride_cm, @Constant int stride_cn,
Copy link
Member

Choose a reason for hiding this comment

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

Why the marking as constants?

Copy link
Author

Choose a reason for hiding this comment

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

Triton will mark these three stride values as constants if they are equal to one at compile time. We mark them as constants directly as our code is not compiled by Triton.

Copy link
Member

Choose a reason for hiding this comment

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

Oh i see, i don't see those equivalent parameters marked as tl.constexpr in the Python matmul example. I think we need the concept of optional constants e.g., a boolean on the @Constant annotation. An optional constant parameter can be associated with type or a constant type.
(Alternatively we could drop the constant annotation, but it seems useful to distinguish various parameters)

@@ -1018,8 +1051,15 @@ public Value dot(TensorType rType, Op.Result r,
TypeElement bType, Value b) {
a = block.context().getValue(a);
b = block.context().getValue(b);

return block.op(TritonOps.dot(rType, a, b));
Object zero;
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment such as

// Computed result is tensor of floats, regardless of inputs

like when we compute the type. Since the result is hard coded we don't need to use reflection and can directly use the constant expression "0.0".

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines +469 to +471
int stride_am, @Constant int stride_ak,
int stride_bk, @Constant int stride_bn,
int stride_cm, @Constant int stride_cn,
Copy link
Member

Choose a reason for hiding this comment

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

Oh i see, i don't see those equivalent parameters marked as tl.constexpr in the Python matmul example. I think we need the concept of optional constants e.g., a boolean on the @Constant annotation. An optional constant parameter can be associated with type or a constant type.
(Alternatively we could drop the constant annotation, but it seems useful to distinguish various parameters)

@@ -107,6 +111,17 @@ void test(CoreOp.FuncOp javaKernel,
return TritonTransformer.tritonModule(javaKernel, JavaType.VOID, argTypes);
});

String mlirText = MLIRGenerator.transform(actualTritonKernel);
Copy link
Member

Choose a reason for hiding this comment

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

We should write the files to the maven target directory. Add the following to the surefire plugin configuration:

                        <systemPropertyVariables>
                            <project.build.directory>${project.build.directory}</project.build.directory>
                        </systemPropertyVariables>

Then replace the code below with:

            Path buildDir = Path.of(System.getProperty("project.build.directory", ""));
            Path mlirDir = buildDir.resolve("mlir");
            try {
                Files.createDirectories(mlirDir);
                Files.writeString(mlirDir.resolve(javaKernelName + ".mlir"), mlirText, StandardOpenOption.CREATE);
            } catch (IOException e) {
                e.printStackTrace();
            }

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants