Text Generation
Transformers
Safetensors
English
mathematics
modular-arithmetic
grokking
scratchpad
length-generalization
Instructions to use ameythakur/SAIR-Modular-Arithmetic-Challenge with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ameythakur/SAIR-Modular-Arithmetic-Challenge with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ameythakur/SAIR-Modular-Arithmetic-Challenge")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ameythakur/SAIR-Modular-Arithmetic-Challenge", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ameythakur/SAIR-Modular-Arithmetic-Challenge with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ameythakur/SAIR-Modular-Arithmetic-Challenge" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ameythakur/SAIR-Modular-Arithmetic-Challenge", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ameythakur/SAIR-Modular-Arithmetic-Challenge
- SGLang
How to use ameythakur/SAIR-Modular-Arithmetic-Challenge with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ameythakur/SAIR-Modular-Arithmetic-Challenge" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ameythakur/SAIR-Modular-Arithmetic-Challenge", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ameythakur/SAIR-Modular-Arithmetic-Challenge" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ameythakur/SAIR-Modular-Arithmetic-Challenge", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use ameythakur/SAIR-Modular-Arithmetic-Challenge with Docker Model Runner:
docker model run hf.co/ameythakur/SAIR-Modular-Arithmetic-Challenge
SAIR
Browse files- README.md +27 -0
- handler.py +50 -0
README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: cc-by-4.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
pipeline_tag: text-generation
|
| 5 |
+
tags:
|
| 6 |
+
- mathematics
|
| 7 |
+
- modular-arithmetic
|
| 8 |
+
- grokking
|
| 9 |
+
- scratchpad
|
| 10 |
license: cc-by-4.0
|
| 11 |
---
|
| 12 |
+
|
| 13 |
+
# SAIR Modular Arithmetic Challenge - Baseline Model
|
| 14 |
+
|
| 15 |
+
## Model Details
|
| 16 |
+
This is the baseline Grokking/Algorithmic model trained for the **SAIR Modular Arithmetic Challenge**. It is an autoregressive decoder-only Transformer designed to solve $(A \times B) \pmod{P}$ without utilizing any external mathematical libraries or hardcoded arithmetic operators.
|
| 17 |
+
|
| 18 |
+
- **Architecture:** Transformer with RoPE / Bit-Serial Algorithmic Decoder
|
| 19 |
+
- **Framework:** PyTorch
|
| 20 |
+
- **Tokenization:** Custom Character-level (`Base10Tokenizer`)
|
| 21 |
+
|
| 22 |
+
## Intended Use
|
| 23 |
+
This model is intended purely for research into algorithmic generalization, grokking, and mathematical reasoning in language models.
|
| 24 |
+
Send a string like `123*456` and the model will generate the Scratchpad trace and the final answer.
|
| 25 |
+
|
| 26 |
+
## Limitations
|
| 27 |
+
As an algorithmic model, the context window bounds the maximum size of the integer that can be processed. If the multiplication trace exceeds the maximum sequence length, the model will fail to output `<EOS>`.
|
| 28 |
+
|
| 29 |
+
## Citation
|
| 30 |
+
If you use this model or the dataset generation logic, please cite the original SAIR Modular Arithmetic Challenge repository.
|
handler.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==============================================================================
|
| 2 |
+
# File: handler.py
|
| 3 |
+
# Description: Core module for SAIR Modular Arithmetic Challenge.
|
| 4 |
+
# Tech Stack: PyTorch 2.0+, Python 3.10+
|
| 5 |
+
# Author: Amey Thakur
|
| 6 |
+
# Profile: https://github.com/Amey-Thakur
|
| 7 |
+
# Repository: https://github.com/Amey-Thakur/SAIR-MODULAR-ARITHMETIC-CHALLENGE
|
| 8 |
+
# License: CC-BY-4.0
|
| 9 |
+
# Date: 2026-07-15
|
| 10 |
+
# ==============================================================================
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import json
|
| 14 |
+
import os
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
# Note: In a real HF deployment, this file sits at the root of the HF repository
|
| 18 |
+
# and the models/tokenizers would be bundled next to it.
|
| 19 |
+
|
| 20 |
+
class EndpointHandler:
|
| 21 |
+
def __init__(self, path=""):
|
| 22 |
+
# self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
# self.tokenizer = Base10Tokenizer()
|
| 24 |
+
# config = TransformerConfig(...)
|
| 25 |
+
# self.model = TransformerRoPE(config)
|
| 26 |
+
# self.model.load_state_dict(load_file(os.path.join(path, "model.safetensors")))
|
| 27 |
+
# self.model.to(self.device)
|
| 28 |
+
# self.model.eval()
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def __call__(self, data: dict):
|
| 32 |
+
"""
|
| 33 |
+
Receives a dictionary with `inputs` (the equation string).
|
| 34 |
+
Returns the predicted modulo output.
|
| 35 |
+
"""
|
| 36 |
+
inputs = data.pop("inputs", None)
|
| 37 |
+
if not inputs:
|
| 38 |
+
return {"error": "No inputs provided. Pass an equation like '123*456'."}
|
| 39 |
+
|
| 40 |
+
# 1. Encode
|
| 41 |
+
# idx = self.tokenizer.encode(inputs)
|
| 42 |
+
# 2. Generate
|
| 43 |
+
# generated = generate(self.model, idx)
|
| 44 |
+
# 3. Decode & Parse Scratchpad
|
| 45 |
+
# answer = extract_answer(generated)
|
| 46 |
+
|
| 47 |
+
# Mocking output for structural completeness
|
| 48 |
+
answer = "0"
|
| 49 |
+
|
| 50 |
+
return [{"generated_text": answer}]
|