From bfbc6ae7d0d2713bbeb9aefa4368570cc9cfe432 Mon Sep 17 00:00:00 2001 From: ModelHub XC Date: Thu, 11 Jun 2026 05:00:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=EF=BC=8C=E7=94=B1ModelHub=20XC=E7=A4=BE=E5=8C=BA=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Model: DireDreadlord/GemCod-Sapphire-270M-XM Source: Original Platform --- .gitattributes | 37 +++++++++++++++ README.md | 105 +++++++++++++++++++++++++++++++++++++++++ chat_template.jinja | 47 ++++++++++++++++++ config.json | 62 ++++++++++++++++++++++++ gemcod_logo_c.png | 3 ++ generation_config.json | 13 +++++ model.safetensors | 3 ++ requirements.txt | 13 +++++ tokenizer.json | 3 ++ tokenizer_config.json | 25 ++++++++++ training_args.bin | 3 ++ 11 files changed, 314 insertions(+) create mode 100644 .gitattributes create mode 100644 README.md create mode 100644 chat_template.jinja create mode 100644 config.json create mode 100644 gemcod_logo_c.png create mode 100644 generation_config.json create mode 100644 model.safetensors create mode 100644 requirements.txt create mode 100644 tokenizer.json create mode 100644 tokenizer_config.json create mode 100644 training_args.bin diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c283d99 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,37 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +tokenizer.json filter=lfs diff=lfs merge=lfs -text +gemcod_logo_c.png filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ebf1b9 --- /dev/null +++ b/README.md @@ -0,0 +1,105 @@ +--- +license: gemma +datasets: +- nphearum/Code-Reasoning-4k +language: +- en +base_model: +- google/gemma-3-270m-it +pipeline_tag: text-generation +tags: +- text-generation-inference +- code +- gemma +- SLM +--- + + +# GemCod270M - Sapphire - XM (gemma-270m-it-code-reasoning v1.1.0 experimental) + +![GemCod logo](./gemcod_logo_c.png) + +GemCod is a lightweight code generation model finetuned using SFT on the base gemma-270m-it model(https://huggingface.co/google/gemma-3-270m-it). It offers accurate and quick(ish) code snippet and long-form code generation in all major programming languages. +It's small size (270M parameters) allows it to run comfortably on laptop grade GPUs. + +The Sapphire model represents the next generation of GemCod agents by integrating COT(Chain Of Thought) reasoning capabilities into the standard coding architecture. This almost completely removes the chances of hallucination and allows the model to give highly detailed and specialized explanations and instructions along with its generations. + +It serves as an upgrade from the previous GemCod-Jade-270M model found here (https://huggingface.co/DireDreadlord/GemCod-Jade-270M) whilst only having minor bloat to inference time and space requirements. +This model also offers rudimentary Q/A and subject matter expert capabilities on code related subjects. + +**This is the XM(Experimental) variant of the model which is trained on only 2,000 steps on SFT; it may give slightly incorrect outputs on long-form generation.** + +--- + + +**Estimated parameters:** ~270M + +**Architecture:** Gemma3 + +**Intended use:** Code snippet and long-form generations from natural language, instruction generation and COT explanations on code snippets + +--- + + +## Training data +- Source: code-reasoning-4k dataset (https://huggingface.co/datasets/nphearum/Code-Reasoning-4k) +- Rows: ~40,000 rows templated with a custom .jinja chat format +- Training: trained for 2,000 steps on an RTX 3050 (4GB VRAM) + + +## Usage + +Install requirements: + +```bash +pip install -r requirements.txt +pip install transformers datasets accelerate safetensors +``` + + +## Usage (Hugging Face Hub) +You can load it directly from HuggingFace: + +```python +from transformers import AutoTokenizer, AutoModelForCausalLM + + +tokenizer = AutoTokenizer.from_pretrained("DireDreadlord/GemCod-Sapphire-270M-XM") +model = AutoModelForCausalLM.from_pretrained("DireDreadlord/GemCod-Sapphire-270M-XM") +model.to(device) +model.eval() +model.resize_token_embeddings(len(tokenizer)) + + +user_prompt = ( + "write a bubble sort algorithm in cpp." + "Please think step by step and show your chain-of-thought before the final code." #<-- comment out this line to disable COT +) + +messages = [{"role": "user", "content": user_prompt}] + + +inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt") +inputs = {k: v.to(device) for k, v in inputs.items()} + + +with torch.no_grad(): + outputs = model.generate( + **inputs, + max_new_tokens=1024, + do_sample=False, + num_beams=1, + pad_token_id=tokenizer.eos_token_id, + eos_token_id=tokenizer.eos_token_id, + use_cache=False, + ) + + +prompt_len = inputs["input_ids"].shape[1] +generated_ids = outputs[0, prompt_len:] +print(tokenizer.decode(generated_ids.tolist(), skip_special_tokens=True)) +``` +**For optimal long-form generation along with COT, set `max_new_tokens=2048`** + +## Limitations +- Model for experimental use only; users should employ it as such under license. \ No newline at end of file diff --git a/chat_template.jinja b/chat_template.jinja new file mode 100644 index 0000000..c5f1365 --- /dev/null +++ b/chat_template.jinja @@ -0,0 +1,47 @@ +{{ bos_token }} +{%- if messages[0]['role'] == 'system' -%} + {%- if messages[0]['content'] is string -%} + {%- set first_user_prefix = messages[0]['content'] + ' + +' -%} + {%- else -%} + {%- set first_user_prefix = messages[0]['content'][0]['text'] + ' + +' -%} + {%- endif -%} + {%- set loop_messages = messages[1:] -%} +{%- else -%} + {%- set first_user_prefix = "" -%} + {%- set loop_messages = messages -%} +{%- endif -%} +{%- for message in loop_messages -%} + {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%} + {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }} + {%- endif -%} + {%- if (message['role'] == 'assistant') -%} + {%- set role = "model" -%} + {%- else -%} + {%- set role = message['role'] -%} + {%- endif -%} + {{ '' + role + ' +' + (first_user_prefix if loop.first else "") }} + {%- if message['content'] is string -%} + {{ message['content'] | trim }} + {%- elif message['content'] is iterable -%} + {%- for item in message['content'] -%} + {%- if item['type'] == 'image' -%} + {{ '' }} + {%- elif item['type'] == 'text' -%} + {{ item['text'] | trim }} + {%- endif -%} + {%- endfor -%} + {%- else -%} + {{ raise_exception("Invalid content type") }} + {%- endif -%} + {{ ' +' }} +{%- endfor -%} +{%- if add_generation_prompt -%} + {{'model +'}} +{%- endif -%} diff --git a/config.json b/config.json new file mode 100644 index 0000000..9f9540f --- /dev/null +++ b/config.json @@ -0,0 +1,62 @@ +{ + "_sliding_window_pattern": 6, + "architectures": [ + "Gemma3ForCausalLM" + ], + "attention_bias": false, + "attention_dropout": 0.0, + "attn_logit_softcapping": null, + "bos_token_id": 2, + "dtype": "bfloat16", + "eos_token_id": 1, + "final_logit_softcapping": null, + "head_dim": 256, + "hidden_activation": "gelu_pytorch_tanh", + "hidden_size": 640, + "initializer_range": 0.02, + "intermediate_size": 2048, + "layer_types": [ + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "full_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "full_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "sliding_attention", + "full_attention" + ], + "max_position_embeddings": 32768, + "model_type": "gemma3_text", + "num_attention_heads": 4, + "num_hidden_layers": 18, + "num_key_value_heads": 1, + "pad_token_id": 0, + "query_pre_attn_scalar": 256, + "rms_norm_eps": 1e-06, + "rope_parameters": { + "full_attention": { + "rope_theta": 1000000.0, + "rope_type": "default" + }, + "sliding_attention": { + "rope_theta": 10000.0, + "rope_type": "default" + } + }, + "sliding_window": 512, + "tie_word_embeddings": true, + "transformers_version": "5.9.0", + "use_bidirectional_attention": false, + "use_cache": false, + "vocab_size": 262145 +} diff --git a/gemcod_logo_c.png b/gemcod_logo_c.png new file mode 100644 index 0000000..9b36899 --- /dev/null +++ b/gemcod_logo_c.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74623faa5b943af3ada82300b8b3058eac1eede3940ec712a3f1f3424538e473 +size 2675237 diff --git a/generation_config.json b/generation_config.json new file mode 100644 index 0000000..1f07995 --- /dev/null +++ b/generation_config.json @@ -0,0 +1,13 @@ +{ + "bos_token_id": 2, + "cache_implementation": "hybrid", + "do_sample": true, + "eos_token_id": [ + 1, + 106 + ], + "pad_token_id": 0, + "top_k": 64, + "top_p": 0.95, + "transformers_version": "5.9.0" +} diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000..c0b5b72 --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad8a061ac9de164890249b5de7e7ab62b37eae0e2be6591f8d3c931d5b154dc9 +size 536224336 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4ee8a29 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,13 @@ +torch +numpy +tiktoken +datasets +transformers +peft +bitsandbytes +tqdm +matplotlib +safetensors +huggingface_hub +accelerate +trl \ No newline at end of file diff --git a/tokenizer.json b/tokenizer.json new file mode 100644 index 0000000..f74d183 --- /dev/null +++ b/tokenizer.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:daab2354f8a74e70d70b4d1f804939b68a8c9624dd06cb7858e52dd8970e9726 +size 33384567 diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..55d93ef --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1,25 @@ +{ + "backend": "tokenizers", + "boi_token": "", + "bos_token": "", + "clean_up_tokenization_spaces": false, + "eoi_token": "", + "eos_token": "", + "image_token": "", + "is_local": false, + "local_files_only": false, + "mask_token": "", + "model_max_length": 1000000000000000019884624838656, + "model_specific_special_tokens": { + "boi_token": "", + "eoi_token": "", + "image_token": "" + }, + "pad_token": "", + "padding_side": "left", + "sp_model_kwargs": null, + "spaces_between_special_tokens": false, + "tokenizer_class": "GemmaTokenizer", + "unk_token": "", + "use_default_system_prompt": false +} diff --git a/training_args.bin b/training_args.bin new file mode 100644 index 0000000..8f3f6f4 --- /dev/null +++ b/training_args.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:87f15dab535d410b5130fef610ff8ddd5c4288b83e66d1609dffd9863b1a3f7d +size 5304