Text Generation
PEFT
Safetensors
GGUF
granite
uno
speculative-decoding
block-diffusion
lora
oflm
openflowlm
npu2
q4nx
conversational
Instructions to use Cyronius/granite-4.2-3b-uno with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Cyronius/granite-4.2-3b-uno with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-4.2-3b") model = PeftModel.from_pretrained(base_model, "Cyronius/granite-4.2-3b-uno") - Notebooks
- Google Colab
- Kaggle
Download code/training/prepare_mix.py from Cyronius/granite-4.2-3b-uno: direct link, hf CLI and curl.
- Browser
- Download file 5.7 kB
-
https://huggingface.co/Cyronius/granite-4.2-3b-uno/resolve/main/code/training/prepare_mix.py
- Command line
-
hf download hf://Cyronius/granite-4.2-3b-uno/code/training/prepare_mix.py
-
curl -L -o prepare_mix.py https://huggingface.co/Cyronius/granite-4.2-3b-uno/resolve/main/code/training/prepare_mix.py
5.7 kB
| """Build the packed OpenThoughts3 + tulu-3 training set in Granite 4.2's chat format.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import random | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from datasets import Dataset, load_dataset | |
| from transformers import AutoTokenizer | |
| from .config import write_json_atomic | |
| from .constants import ( | |
| CHAT_DATASET_ID, | |
| CHAT_DATASET_REVISION, | |
| CHAT_TOKEN_SHARE, | |
| CUTOFF_LENGTH, | |
| DEFAULT_DATASET_ID, | |
| DEFAULT_DATASET_REVISION, | |
| DEFAULT_MODEL_ID, | |
| DEFAULT_MODEL_REVISION, | |
| DEFAULT_SEED, | |
| PREPROCESS_TOKEN_ALIGNMENT, | |
| PREPROCESSED_MAX_LENGTH, | |
| ) | |
| from .data import encode_granite_conversation, hash_hf_dataset, normalize_messages, pack_group, plan_packing | |
| OPENTHOUGHTS_SHARDS = 120 | |
| OPENTHOUGHTS_TOKENS_PER_SHARD = 35_000_000 | |
| def _encode_batch(batch, tokenizer): | |
| out = {"input_ids": [], "labels": [], "length": []} | |
| columns = list(batch.keys()) | |
| for index in range(len(batch[columns[0]])): | |
| row = {column: batch[column][index] for column in columns} | |
| try: | |
| encoded = encode_granite_conversation(normalize_messages(row), tokenizer) | |
| except (ValueError, KeyError): | |
| continue | |
| out["input_ids"].append(encoded["input_ids"]) | |
| out["labels"].append(encoded["labels"]) | |
| out["length"].append(len(encoded["input_ids"])) | |
| return out | |
| def _encode(dataset, tokenizer, num_proc): | |
| return dataset.map( | |
| _encode_batch, | |
| batched=True, | |
| batch_size=256, | |
| num_proc=num_proc, | |
| remove_columns=dataset.column_names, | |
| fn_kwargs={"tokenizer": tokenizer}, | |
| ) | |
| def _take_tokens(dataset, budget): | |
| total = 0 | |
| for index, length in enumerate(dataset["length"]): | |
| total += length | |
| if total >= budget: | |
| return dataset.select(range(index + 1)), total | |
| return dataset, total | |
| def _packed_rows(sources, groups): | |
| for group in groups: | |
| yield pack_group(sources[source][index] for source, index in group) | |
| def main(argv=None): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--output-dir", required=True, type=Path) | |
| parser.add_argument("--total-tokens", type=float, default=3.6e9, help="Real (unpadded) tokens to keep.") | |
| parser.add_argument("--num-proc", type=int, default=20) | |
| parser.add_argument("--max-rows", type=int, help="Cap rows per source before encoding (smoke runs).") | |
| args = parser.parse_args(argv) | |
| tokenizer = AutoTokenizer.from_pretrained(DEFAULT_MODEL_ID, revision=DEFAULT_MODEL_REVISION) | |
| chat_budget = int(args.total_tokens * CHAT_TOKEN_SHARE) | |
| reasoning_budget = int(args.total_tokens) - chat_budget | |
| shard_count = min(OPENTHOUGHTS_SHARDS, int(reasoning_budget / OPENTHOUGHTS_TOKENS_PER_SHARD * 1.15) + 1) | |
| # Evenly spaced shards, in case the source is ordered by domain. | |
| shard_ids = sorted({round(i * (OPENTHOUGHTS_SHARDS - 1) / max(shard_count - 1, 1)) for i in range(shard_count)}) | |
| reasoning = load_dataset( | |
| DEFAULT_DATASET_ID, | |
| revision=DEFAULT_DATASET_REVISION, | |
| data_files=[f"data/train-{i:05d}-of-{OPENTHOUGHTS_SHARDS:05d}.parquet" for i in shard_ids], | |
| split="train", | |
| num_proc=args.num_proc, | |
| # The card records all 1.2M rows; a shard subset would fail the split-size check. | |
| verification_mode="no_checks", | |
| ).shuffle(seed=DEFAULT_SEED) | |
| chat = load_dataset( | |
| CHAT_DATASET_ID, revision=CHAT_DATASET_REVISION, split="train", num_proc=args.num_proc | |
| ).shuffle(seed=DEFAULT_SEED) | |
| if args.max_rows: | |
| reasoning = reasoning.select(range(min(args.max_rows, len(reasoning)))) | |
| chat = chat.select(range(min(args.max_rows, len(chat)))) | |
| reasoning, reasoning_tokens = _take_tokens(_encode(reasoning, tokenizer, args.num_proc), reasoning_budget) | |
| chat, chat_tokens = _take_tokens(_encode(chat, tokenizer, args.num_proc), chat_budget) | |
| # Packing each source on its own keeps rows full: long traces would otherwise strand short chats. | |
| groups = [[(0, i) for i in group] for group in plan_packing(reasoning["length"])] | |
| groups += [[(1, i) for i in group] for group in plan_packing(chat["length"])] | |
| random.Random(DEFAULT_SEED).shuffle(groups) | |
| sources = (reasoning.remove_columns("length"), chat.remove_columns("length")) | |
| packed = Dataset.from_generator(_packed_rows, gen_kwargs={"sources": sources, "groups": groups}) | |
| args.output_dir.mkdir(parents=True, exist_ok=True) | |
| packed.save_to_disk(str(args.output_dir)) | |
| manifest = { | |
| "version": 1, | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| "sources": [ | |
| {"dataset_id": DEFAULT_DATASET_ID, "revision": DEFAULT_DATASET_REVISION, "shards": shard_ids, | |
| "examples": len(reasoning), "tokens": reasoning_tokens}, | |
| {"dataset_id": CHAT_DATASET_ID, "revision": CHAT_DATASET_REVISION, | |
| "examples": len(chat), "tokens": chat_tokens}, | |
| ], | |
| "chat_token_share": CHAT_TOKEN_SHARE, | |
| "tokenizer": {"model_id": DEFAULT_MODEL_ID, "revision": DEFAULT_MODEL_REVISION}, | |
| "preprocessing": { | |
| "cutoff_length": CUTOFF_LENGTH, | |
| "token_alignment": PREPROCESS_TOKEN_ALIGNMENT, | |
| "max_preprocessed_length": PREPROCESSED_MAX_LENGTH, | |
| "packing": "greedy, position ids restart per example", | |
| "seed": DEFAULT_SEED, | |
| }, | |
| "hash": hash_hf_dataset(packed), | |
| } | |
| write_json_atomic(args.output_dir / "uno_dataset_manifest.json", manifest) | |
| print(json.dumps({k: manifest[k] for k in ("sources", "hash")}, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |