From 6a699f6410063fa8bbe9a8c32468d775f6202a34 Mon Sep 17 00:00:00 2001 From: Marcel van Workum Date: Mon, 5 May 2025 16:10:17 -0700 Subject: [PATCH 1/2] Add script and documentation on running formatting --- README.md | 26 ++++++++++++++++++++++++++ format.sh | 7 +++++++ pyproject.toml | 23 ++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 format.sh diff --git a/README.md b/README.md index 11456ca..86b0440 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,32 @@ If you find this work helpful, please consider citing our technical report: } ``` +## Contributing + +We welcome contributions to Cube! Here's how you can set up your development environment and follow our code style guidelines: + +### Development Setup + +1. Clone the repository and install development dependencies: + +```bash +git clone https://github.com/Roblox/cube.git +cd cube +pip install -e ".[dev]" +``` + +### Code Formatting and Linting + +We use [ruff](https://github.com/astral-sh/ruff) for both code formatting and linting to maintain consistent code style and quality. + +To run the formatter and linter: + +```bash +./format.sh +``` + +Please format your code before submitting pull requests. + ## Acknowledgements We would like to thank the contributors of [TRELLIS](https://github.com/microsoft/TRELLIS), [CraftsMan3D](https://github.com/wyysf-98/CraftsMan3D), [threestudio](https://github.com/threestudio-project/threestudio), [Hunyuan3D-2](https://github.com/Tencent/Hunyuan3D-2), [minGPT](https://github.com/karpathy/minGPT), [dinov2](https://github.com/facebookresearch/dinov2), [OptVQ](https://github.com/zbr17/OptVQ), [1d-tokenizer](https://github.com/bytedance/1d-tokenizer) diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..1535cf7 --- /dev/null +++ b/format.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +python -m ruff format . +python -m ruff check . --fix +# Sort imports +python -m ruff check . --select I --fix \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3300671..a7f54cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,9 +30,30 @@ dependencies = [ ] [project.optional-dependencies] meshlab = ["pymeshlab"] -lint = ["ruff==0.9.10"] +dev = ["ruff==0.9.10"] [tool.setuptools.packages.find] where = ["cube3d"] include = ["cube/*"] namespaces = false + +[tool.ruff] +line-length = 88 +indent-width = 4 + +exclude = [ + ".git", + ".ruff_cache", + ".venv", + "build", + "dist", + "*.ipynb" +] + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" +line-ending = "auto" + +[tool.ruff.lint] +ignore = ["E741", "E722"] From f8355fb3ff4fb4e994d8a87403ab1ffc42083fef Mon Sep 17 00:00:00 2001 From: Marcel van Workum Date: Mon, 5 May 2025 16:11:05 -0700 Subject: [PATCH 2/2] Run linting --- cube3d/generate.py | 3 ++- cube3d/inference/engine.py | 22 +++++++++---------- cube3d/inference/logits_postprocesses.py | 6 ++--- cube3d/inference/utils.py | 3 +-- cube3d/model/autoencoder/one_d_autoencoder.py | 3 +-- cube3d/model/autoencoder/spherical_vq.py | 2 -- cube3d/renderer/blender_script.py | 2 +- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/cube3d/generate.py b/cube3d/generate.py index 3d47bc6..ee8ab87 100644 --- a/cube3d/generate.py +++ b/cube3d/generate.py @@ -13,6 +13,7 @@ ) from cube3d.renderer import renderer + def generate_mesh( engine, prompt, @@ -127,7 +128,7 @@ def generate_mesh( engine = Engine( args.config_path, args.gpt_ckpt_path, args.shape_ckpt_path, device=device ) - + # Generate meshes based on input source obj_path = generate_mesh( engine, diff --git a/cube3d/inference/engine.py b/cube3d/inference/engine.py index d2bd21f..abad9bf 100644 --- a/cube3d/inference/engine.py +++ b/cube3d/inference/engine.py @@ -194,7 +194,7 @@ def run_gpt( embed.device, ) with torch.autocast(self.device.type, dtype=torch.bfloat16): - for i in tqdm(range(self.max_new_tokens), desc=f"generating"): + for i in tqdm(range(self.max_new_tokens), desc="generating"): curr_pos_id = torch.tensor([i], dtype=torch.long, device=embed.device) logits = self.gpt_model( embed_buffer, @@ -276,7 +276,7 @@ def t2s( guidance_scale (float, optional): The scale of guidance for the GPT model. Default is 3.0. resolution_base (float, optional): The base resolution for the shape decoder. Default is 8.0. chunk_size (int, optional): The chunk size for processing the shape decoding. Default is 100,000. - top_p (float, optional): The cumulative probability threshold for nucleus sampling. + top_p (float, optional): The cumulative probability threshold for nucleus sampling. If None, argmax selection is performed (deterministic generation). Otherwise, smallest set of tokens with cumulative probability ≥ top_p are kept (stochastic generation). Returns: mesh_v_f: The generated 3D mesh vertices and faces. @@ -304,9 +304,9 @@ def __init__( device (torch.device): The device to run the inference on (e.g., CPU or CUDA). """ - assert ( - device.type == "cuda" - ), "EngineFast is only supported on cuda devices, please use Engine on non-cuda devices" + assert device.type == "cuda", ( + "EngineFast is only supported on cuda devices, please use Engine on non-cuda devices" + ) super().__init__(config_path, gpt_ckpt_path, shape_ckpt_path, device) @@ -428,11 +428,11 @@ def _set_curr_pos_id(self, pos: int): ) def run_gpt( - self, - prompts: list[str], - use_kv_cache: bool, + self, + prompts: list[str], + use_kv_cache: bool, guidance_scale: float = 3.0, - top_p: float = None + top_p: float = None, ): """ Runs the GPT model to generate text based on the provided prompts. @@ -479,9 +479,7 @@ def run_gpt( next_embed = next_embed.repeat(2, 1, 1) self.embed_buffer[:, input_seq_len, :].copy_(next_embed.squeeze(1)) - for i in tqdm( - range(1, self.max_new_tokens), desc=f"generating" - ): + for i in tqdm(range(1, self.max_new_tokens), desc="generating"): self._set_curr_pos_id(i) self.graph.replay() diff --git a/cube3d/inference/logits_postprocesses.py b/cube3d/inference/logits_postprocesses.py index 28a81cd..5ead889 100644 --- a/cube3d/inference/logits_postprocesses.py +++ b/cube3d/inference/logits_postprocesses.py @@ -30,9 +30,9 @@ def top_p_filtering(logits, top_p: float = 1.0): def process_logits( - logits, - top_p: float = None, - ): + logits, + top_p: float = None, +): """ Process logits by optionally applying nucleus (top-p) filtering and token selection. diff --git a/cube3d/inference/utils.py b/cube3d/inference/utils.py index 4a810c0..27952dd 100644 --- a/cube3d/inference/utils.py +++ b/cube3d/inference/utils.py @@ -1,5 +1,4 @@ -import logging -from typing import Any, Optional +from typing import Any import torch from omegaconf import DictConfig, OmegaConf diff --git a/cube3d/model/autoencoder/one_d_autoencoder.py b/cube3d/model/autoencoder/one_d_autoencoder.py index a0c617d..2ee62e8 100644 --- a/cube3d/model/autoencoder/one_d_autoencoder.py +++ b/cube3d/model/autoencoder/one_d_autoencoder.py @@ -1,5 +1,4 @@ import logging -import sys from dataclasses import dataclass, field from functools import partial from typing import List, Optional, Tuple @@ -632,7 +631,7 @@ def extract_geometry( progress_bar = tqdm( range(0, xyz_samples.shape[0], chunk_size), - desc=f"extracting geometry", + desc="extracting geometry", unit="chunk", ) for start in progress_bar: diff --git a/cube3d/model/autoencoder/spherical_vq.py b/cube3d/model/autoencoder/spherical_vq.py index 36cffc1..7a868cb 100644 --- a/cube3d/model/autoencoder/spherical_vq.py +++ b/cube3d/model/autoencoder/spherical_vq.py @@ -1,4 +1,3 @@ -import sys from typing import Literal, Optional import torch @@ -62,7 +61,6 @@ def get_codebook(self): return self.norm(self.cb_norm(self.codebook.weight)) @torch.no_grad() - def lookup_codebook(self, q: torch.Tensor): """ Perform a lookup in the codebook and process the result. diff --git a/cube3d/renderer/blender_script.py b/cube3d/renderer/blender_script.py index 046e030..3ae1d14 100644 --- a/cube3d/renderer/blender_script.py +++ b/cube3d/renderer/blender_script.py @@ -556,7 +556,7 @@ def enable_gpus(device_type, use_cpus=False): if device_type == "CPU": return [] else: - raise RuntimeError(f"No devices detected, set use_cpus to True") + raise RuntimeError("No devices detected, set use_cpus to True") assert device_type in [ "CUDA",