Skip to content

[ROCm] add rocm dockerfile - #4223

Closed
danielhanchen wants to merge 15 commits into
mainfrom
dh/recover-3324-rocm-dockerfile
Closed

danielhanchen wants to merge 15 commits into
mainfrom
dh/recover-3324-rocm-dockerfile

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

Replacement for #3324 due to Studio rebasing.

Summary

Adds one new file, docker/Dockerfile.rocm, for running Unsloth on AMD Instinct MI300 series hardware. Nothing else in the repository changes: the diff is docker/Dockerfile.rocm, +69/-0.

The image layers on top of a ROCm vLLM base, builds bitsandbytes from source against HIP for gfx942, installs Unsloth and unsloth_zoo from git without disturbing the base image's ROCm PyTorch, and ends with a build-time check that fails the build if the ROCm bitsandbytes library did not actually get compiled and cannot be loaded.

Build it with:

docker build --no-cache -f docker/Dockerfile.rocm -t <Your Desired Image Name> .

What the image does

Base. rocm/vllm-dev:nightly_main_20250914, overridable through the BASE_DOCKER build arg. The working directory is /unsloth-workspace. System packages (zip, unzip, wget, build-essential, cmake) are installed in a single RUN so the apt index cannot go stale between the update and the install, with /var/lib/apt/lists removed in the same layer.

bitsandbytes, built from source for HIP. Configured with -DCOMPUTE_BACKEND=hip and -DBNB_ROCM_ARCH=gfx942, then cmake --build -j"$(nproc)", then installed. The source is pinned to commit 713a3b83e405d32449e1cb392e5e25985ab927c6, which is the merge commit of bitsandbytes-foundation/bitsandbytes#1887, "[ROCm] Enable blocksize 32 4-bit quantization and GEMV kernels on AMD CDNA". That PR is the floor: builds from before it are wrong at 4-bit GEMV decode on CDNA, where the wavefront size is 64. The checkout is a shallow git init plus fetch --depth 1 <commit> rather than a full clone, and .git and build/ are both removed in the same layer.

bitsandbytes is installed plainly rather than with -e, and the Dockerfile says why inline: upstream reserves editable installs for bitsandbytes development, and scikit-build-core 1.0.0 rejects an editable install unless pyproject.toml opts into editable.mode = "inplace", which this pin predates. cmake has already placed the .so into the package directory by that point, so setuptools package-data ships it as a built artifact.

Unsloth and unsloth_zoo. Both are shallow-cloned by branch and installed editable. Unsloth uses the [huggingfacenotorch] extra specifically, not a bare -e ., because that extra omits torch and torchvision and so leaves the base image's ROCm PyTorch in place. A bare -e . would pull a CUDA PyTorch over the top and the image could not train. Note that [huggingfacenotorch] does declare unsloth_zoo, so a PyPI unsloth_zoo is installed first and is then superseded by the editable clone in the following layer.

Build args. BASE_DOCKER, COMPUTE_BACKEND, BNB_ROCM_ARCHS, BNB_COMMIT, BNB_REPO, UNSLOTH_REPO, UNSLOTH_BRANCH, UNSLOTH_ZOO_REPO, UNSLOTH_ZOO_BRANCH. Retargeting another CDNA part is --build-arg BNB_ROCM_ARCHS=<arch>.

The verification layer

The final RUN is the part worth reviewing. It is a real guard, and it exists because the two obvious ways of checking both return green on a broken image:

  • pip show reads dist-info only. It stayed green on an image whose freshly compiled HIP library was missing entirely.
  • bitsandbytes' own self-report is no help either. With no AMD GPU on the builder it falls back to libbitsandbytes_cpu.so and reports success.

So the check goes at the library directly. It asserts that at least one libbitsandbytes_rocm*.so exists in the installed package directory, then ctypes.CDLLs every libbitsandbytes*.so it finds there, so a library that is present but unloadable is also caught. For Unsloth and unsloth_zoo it uses importlib.util.find_spec rather than importing them, because unsloth_zoo refuses to import without a torch accelerator and there is no AMD GPU on the builder.

The known gap is documented in the file rather than papered over: a build for the wrong architecture still loads fine at this point and only fails once a 4-bit GEMV runs on real AMD silicon. ctypes.CDLL resolves the library, it does not validate the code objects inside it against the target.

Scope and limits

  • Default arch is gfx942, which covers MI300A, MI300X and MI325X. CDNA4 parts (MI350X, MI355X, gfx950) need BNB_ROCM_ARCHS overridden and are not otherwise exercised here.
  • The base tag is a dated ROCm nightly, not a stable ROCm release, so it is a moving reference point pinned to one day.
  • No CI is added by this PR and nothing builds this image automatically. There is no workflow change in the diff.
  • The verification layer runs on the builder, which has no AMD GPU, so it can prove the ROCm library was built and is loadable but cannot prove it runs correctly on hardware.

Attribution

Original work by @billishyahao in #3324. That PR was closed because of a Studio rebase; this branch carries the same contribution forward, plus the bitsandbytes commit pin, the [huggingfacenotorch] install and the verification layer described above.

(cherry picked from commit 6d6ab6b)
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a dedicated Dockerfile to streamline the setup of a development environment for ROCm-enabled Instinct mi30x GPUs. Its primary goal is to simplify the process of getting started with Unsloth and BitsAndBytes on the ROCm platform, thereby improving the overall user experience for developers targeting these specific hardware configurations.

Highlights

  • ROCm Dockerfile Added: A new Dockerfile.rocm has been introduced to provide a standardized build environment for Instinct mi30x GPUs on the ROCm platform. This Dockerfile serves as a replacement for a previous pull request ([ROCm] add rocm dockerfile #3324).
  • BitsAndBytes Integration: The Dockerfile includes steps to clone, build, and install the bitsandbytes library, configured with the HIP compute backend and targeting the gfx942 GPU architecture.
  • Unsloth and Unsloth-Zoo Setup: The environment is further enhanced by cloning and installing both the unsloth and unsloth-zoo repositories, ensuring a complete development setup for these tools.
Changelog
  • docker/Dockerfile.rocm
    • Added a new Dockerfile for ROCm-based environments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a Dockerfile for ROCm. The review identifies a few areas for improvement. There is a critical issue with a likely typo in a future date in the base image tag, which will probably cause the build to fail. Additionally, there are several suggestions to optimize the Dockerfile for smaller image size and faster builds by combining RUN instructions for apt and pip commands, and using a more efficient method to clone a specific git commit. These changes follow Docker best practices.

Comment thread docker/Dockerfile.rocm Resolved
Comment thread docker/Dockerfile.rocm Resolved
Comment thread docker/Dockerfile.rocm Resolved
Comment thread docker/Dockerfile.rocm Resolved
@nidhishgajjar

This comment was marked as low quality.

Combine apt-get update/install into one RUN with --no-install-recommends and
list cleanup (a cached standalone update layer goes stale for later installs),
fetch only the pinned bitsandbytes commit instead of cloning the default branch
first, and fold the three pip verification probes into a single layer.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 976cfedf31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: abfe2b680c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docker/Dockerfile.rocm Resolved
Comment thread docker/Dockerfile.rocm Resolved
Comment thread docker/Dockerfile.rocm Resolved
…OCm image

Two review items, both reproduced before fixing.

The pinned bitsandbytes commit (0.48.0.dev0, 2025-09-15) predates bnb PR
1887, which enabled the 4-bit GEMV kernels on AMD CDNA; every build at or
below 0.49.2 NaNs at the GEMV decode shape on AMD GPUs, the exact floor
install.sh already enforces for its ROCm wheel. BNB_COMMIT now defaults
to the PR 1887 merge commit (0.50.0.dev0), which carries the fix.

The unsloth install used bare pip install -e ., which pulls only the CLI
dependencies (typer, rich, pydantic, pyyaml, nest-asyncio); the pip show
probe passed on metadata alone while transformers, trl, peft, datasets,
and accelerate were all absent, so the image built green but could not
train. It now installs the huggingfacenotorch extra, which delivers the
full HF training stack without torch or torchvision so the ROCm torch in
the vLLM base image is never clobbered by a PyPI CPU/CUDA build.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 66f912625d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7815f2f08e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Main moved the llama_extra_args pass-through inheritance out of the
GGUF branch into _resolve_inherited_extra_args, which runs before it,
so the source-order assertion's "if request.llama_extra_args is None"
anchor no longer exists inside the branch and the check failed after
the main merge. The test now asserts the same property in the current
shape: inheritance before the GGUF branch (a carried --no-mmproj still
shapes the hub guard's companion requirement), and marker, hub guard,
unload in order within the branch. Full file passes (32 tests).
…tion

source.index("_resolve_inherited_extra_args(") matched the function
definition, which always precedes the endpoint, so the ordering
assertion was vacuously true. Anchoring on "= _resolve_inherited_
extra_args(" pins the first call site inside the load endpoint (line
4505), which is the statement whose position relative to the GGUF
branch the test is meant to guard. 32 tests pass.
Main fixed the stale ordering assertion in PR 7252; adopting its
version verbatim removes this file from the branch diff entirely and
avoids a conflict on the next main merge. 32 tests pass.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c6fb65fa1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docker/Dockerfile.rocm Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5c6fb65fa1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…dockerfile

One conflict: studio/backend/tests/test_gguf_load_cache_reuse.py. Took
main's assertion, which anchors on the _resolve_inherited_extra_args
call site. This branch does not touch studio/backend/routes/inference.py,
so the merged source is main's, and that is the landmark it now carries.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 82d126c0c8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Drop the commented-out GPU_ARCH arg, the shallow-fetch note, and the
install verification banner, and fold the huggingfacenotorch note onto
one line. The bitsandbytes commit pin note stays: it records the
upstream 4-bit GEMV NaN behaviour on AMD.

No build instruction changed.
…le to fail

The image did not build. bitsandbytes pins scikit-build-core unbounded in
build-system.requires, so the builder always resolves the newest release.
1.0.0 (2026-07-06) added a PEP 660 gate to the setuptools plugin backend that
bitsandbytes uses, rejecting editable installs unless pyproject sets
editable.mode = "inplace". The pin here is from March and predates upstream
adding that line, so pip install -e . dies in prepare_metadata_for_build_editable
with "setuptools editable installs require editable.mode = 'inplace'".
Verified by version bisection in the image: 0.11.6 and 0.12.2 install cleanly,
1.0.0, 1.0.1 and 1.0.3 all fail.

Install non-editable instead. Upstream's own install docs annotate the -e as
being for bitsandbytes development and say to leave it out otherwise, and an
editable checkout buys nothing in an image while tying the build to a
scikit-build-core knob that keeps moving. CMakeLists writes the library
straight into the package dir, so setuptools package-data ships exactly what
cmake built: the installed libbitsandbytes_rocm64.so is md5 identical to the
compiled one and its .hip_fatbin still holds a single gfx942 bundle.

The verification step could not fail. pip show passes on dist-info alone, so a
missing or unloadable HIP library left it green on precisely the broken image
it exists to catch. Load the artifacts instead. bitsandbytes cannot self-report
here: with no AMD GPU on the builder get_cuda_specs returns None, it loads
libbitsandbytes_cpu.so and hands back a healthy BNBNativeLibrary even after the
ROCm library is deleted, so the check globs for libbitsandbytes_rocm*.so beside
the installed package and ctypes.CDLL loads it. unsloth_zoo raises
NotImplementedError on import without a torch accelerator, so it and unsloth
are resolved with importlib.util.find_spec rather than executed.

Deleting the library now fails the check; the intact image passes.

The amdgpu-arch warning at configure time is benign and left alone. It comes
from hip-config-amd.cmake, pulled in by find_package(hipblas), and only governs
--offload-arch on the hip::device interface for CXX sources. bitsandbytes links
hip::host and compiles its kernels as HIP, taking the arch from
CMAKE_HIP_ARCHITECTURES, which BNB_ROCM_ARCH sets before enable_language(HIP).
The built library contains gfx942 and no gfx906.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: ba209f8539

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 27, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 6f5f2e4ef4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Checked docker/Dockerfile.rocm against main, which has no docker build path today, and the bitsandbytes pin now sits on the 0.50.0.dev0 commit that fixes 4-bit GEMV decode on CDNA. Will get this reviewed.

@LeoBorcherding could you take a look at this one?

Drop the self-explanatory note on the apt layer and collapse the
bitsandbytes install and final verification blocks to one line each
where they still read clearly. The reasons behind the pinned commit,
the plain (non-editable) install, the huggingfacenotorch extra and the
limits of the verification layer are kept. No instruction changes.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-04T05:09:53.708953Z 79d2484 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

Closing this rather than merging it.

The Dockerfile itself is sound and was driven to convergence, but the image is not something we are set up to stand behind. Nothing in CI builds it, so it would go stale silently. The base is a dated ROCm nightly pinned to a single day rather than a stable release. The default gfx942 covers only MI300A, MI300X and MI325X, with CDNA4 parts needing an override and no coverage here. And the build-time verification can prove the ROCm bitsandbytes library was compiled and is loadable, but not that it runs correctly, because there is no AMD GPU on the builder.

Carrying an unbuilt, unexercised image that claims MI300 support is worse than not shipping one, so it is better to close this until there is a maintained ROCm lane to attach it to.

The work is preserved here and in #3324 if we pick this up again. Thanks for the original contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants