Skip to content

Commit

Permalink
feat: Dev Container for consistent dev setup
Browse files Browse the repository at this point in the history
Added a Dev Container configuration to streamline
development and onboarding. This setup ensures a
consistent, isolated environment with all necessary
tools and dependencies for building and running
Triton-CPU. The configuration supports use in both
VS Code locally and GitHub Codespaces.

Signed-off-by: Maryam Tahhan <[email protected]>
  • Loading branch information
maryamtahhan committed Nov 14, 2024
1 parent c0b74b0 commit 899b376
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .devcontainer/triton-cpu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM registry.access.redhat.com/ubi9/python-312

ARG USERNAME=1001
ARG USER_UID=1000
ARG USER_GID=$USER_UID

USER 0
COPY user.sh user.sh
# Create the user
RUN ./user.sh -u $USERNAME -g $USER_GID
# Set the user
USER $USERNAME

ENV PYTHON_VERSION=3.12 \
PATH=$HOME/.local/bin/:$PATH \
PYTHONUNBUFFERED=1 \
TRITON_CPU_BACKEND=1

# install dependencies
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install ninja cmake wheel pybind11;
RUN pip install pre-commit
RUN echo "export MAX_JOBS=$(nproc --all)" >> "${HOME}"/.bashrc
38 changes: 38 additions & 0 deletions .devcontainer/triton-cpu/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "Triton-CPU",
"build": {
"dockerfile": "Dockerfile",
"args": {
// root (not recommended)
"USERNAME": "${localEnv:USER}",
"USER_UID": 1000,
"USER_GID": 1000
}
},
"remoteUser": "${localEnv:USER}",
"containerUser": "${localEnv:USER}",
// Add the IDs of extensions you want installed when the container is created.
"customizations": {
"vscode": {
"extensions": [
"ms-python.debugpy",
"ms-python.flake8",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools-themes",
"twxs.cmake"
],
}
},
"features": {},
"postStartCommand": "${containerWorkspaceFolder}/.devcontainer/triton-cpu/postStartCommand.sh",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind",
"workspaceFolder": "/workspace",
"hostRequirements": {
"cpus": 4,
"gpu": false
}
}
20 changes: 20 additions & 0 deletions .devcontainer/triton-cpu/postStartCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright (C) 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
git submodule init
git submodule update
pre-commit install
100 changes: 100 additions & 0 deletions .devcontainer/triton-cpu/user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/sh

# Copyright (C) 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
set -euo pipefail

username=""
userid=""

usage() {
cat >&2 <<EOF
Usage: $0
-u | --user <username>
-g | --gid <userid>
EOF
exit 1
}

# Parse command-line arguments
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@")
if [ $? -ne 0 ]; then usage; fi

eval set -- "$args"
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
usage
;;
-u | --user)
username=$2
shift 2
;;
-g | --gid)
userid=$2
shift 2
;;
--)
shift
break
;;
*)
echo "Unsupported option: $1" >&2
usage
;;
esac
done

# Validate required parameters
if [ -z "$username" ] || [ -z "$userid" ]; then
echo "Error: --user and --gid are required." >&2
usage
fi

USER_NAME="$username"
USER_UID="$userid"
USER_GID="$USER_UID"
HOME_DIR="/home/$USER_NAME"

# Exit if the user is root
if [ "$USER_NAME" = "root" ]; then
exit 0
fi

if ! [ $(getent group $USER_NAME) ]; then
groupadd --gid $USER_GID $USER_NAME
fi

if ! [ $(getent passwd $USER_NAME) ]; then
useradd --uid $USER_UID --gid $USER_GID -m $USER_NAME
fi

# Ensure $HOME exists when starting
if [ ! -d "${HOME}" ]; then
mkdir -p "${HOME}"
fi

# Add current (arbitrary) user to /etc/passwd and /etc/group
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user} user:${HOME}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
fi

# Fix up permissions
chown $USER_NAME:$USER_GID -R /home/$USER_NAME
chown $USER_NAME:$USER_GID -R /opt
mkdir -p /run/user/$USER_UID
chown $USER_NAME:$USER_GID /run/user/$USER_UID
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ subdirectory (similar to how GPU vendors are supported today). We're starting
with a clone to give ourselves maximum development flexibility as this project
gets off the ground!

# How to use it?
## How to use it?

Build it like a normal Triton, but just pass TRITON_CPU_BACKEND=1 to use the CPU backend over a GPU backend, if any.

```
TRITON_CPU_BACKEND=1 python3 tutorials/01-vector-add.py
```

**NOTE: It's still work in progress.**

---

# Upstream README
Expand Down
1 change: 1 addition & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ def get_git_commit_hash(length=8):
"matplotlib",
"pandas",
"tabulate",
"torch",
],
},
)

0 comments on commit 899b376

Please sign in to comment.