Text Generation
Transformers
Turkish
English
armorm
text-classification
reward-model
turkish
legal
turkish-legal
mecellem
armo
reward
evaluation
TRUBA
MN5
conversational
custom_code
Instructions to use newmindai/Muhakim with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use newmindai/Muhakim with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="newmindai/Muhakim", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained("newmindai/Muhakim", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use newmindai/Muhakim with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "newmindai/Muhakim" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Muhakim", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/newmindai/Muhakim
- SGLang
How to use newmindai/Muhakim 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 "newmindai/Muhakim" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Muhakim", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "newmindai/Muhakim" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Muhakim", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use newmindai/Muhakim with Docker Model Runner:
docker model run hf.co/newmindai/Muhakim
File size: 6,392 Bytes
6c75793 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, List
from dataclasses import dataclass
from transformers import PreTrainedModel, PretrainedConfig, LlamaModel, LlamaConfig
from transformers.modeling_outputs import ModelOutput
class ArmoRMConfig(PretrainedConfig):
model_type = "armorm"
def __init__(
self,
vocab_size=128256,
hidden_size=4096,
intermediate_size=14336,
num_hidden_layers=32,
num_attention_heads=32,
num_key_value_heads=8,
hidden_act="silu",
max_position_embeddings=131072,
initializer_range=0.02,
rms_norm_eps=1e-5,
use_cache=True,
rope_theta=500000.0,
attention_bias=False,
attention_dropout=0.0,
mlp_bias=False,
num_objectives=5,
objective_names=None,
gating_hidden_dim=1024,
gating_num_layers=4,
temperature=10.0,
**kwargs,
):
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.num_key_value_heads = num_key_value_heads
self.hidden_act = hidden_act
self.max_position_embeddings = max_position_embeddings
self.initializer_range = initializer_range
self.rms_norm_eps = rms_norm_eps
self.use_cache = use_cache
self.rope_theta = rope_theta
self.attention_bias = attention_bias
self.attention_dropout = attention_dropout
self.mlp_bias = mlp_bias
self.num_objectives = num_objectives
self.objective_names = objective_names or [
"statute_reference",
"legal_accuracy",
"case_law_reference",
"linguistic_coherence",
"depth_coverage"
]
self.gating_hidden_dim = gating_hidden_dim
self.gating_num_layers = gating_num_layers
self.temperature = temperature
super().__init__(**kwargs)
@dataclass
class ArmoRMOutput(ModelOutput):
logits: Optional[torch.FloatTensor] = None
score: Optional[torch.FloatTensor] = None
rewards: Optional[torch.FloatTensor] = None
gating_output: Optional[torch.FloatTensor] = None
class GatingNetwork(nn.Module):
def __init__(self, in_features, out_features, hidden_dim=1024, num_layers=4, temperature=10.0):
super().__init__()
self.temperature = temperature
layers = []
current_dim = in_features
for i in range(num_layers - 1):
layers.append(nn.Linear(current_dim, hidden_dim))
current_dim = hidden_dim
layers.append(nn.Linear(current_dim, out_features))
self.layers = nn.ModuleList(layers)
def forward(self, x):
for i, layer in enumerate(self.layers):
x = layer(x)
if i < len(self.layers) - 1:
x = F.relu(x)
x = F.softmax(x / self.temperature, dim=-1)
return x
class ArmoRMForSequenceClassification(PreTrainedModel):
config_class = ArmoRMConfig
base_model_prefix = "model"
def __init__(self, config):
super().__init__(config)
self.config = config
# LlamaModel as base
llama_config = LlamaConfig(
vocab_size=config.vocab_size,
hidden_size=config.hidden_size,
intermediate_size=config.intermediate_size,
num_hidden_layers=config.num_hidden_layers,
num_attention_heads=config.num_attention_heads,
num_key_value_heads=config.num_key_value_heads,
hidden_act=config.hidden_act,
max_position_embeddings=config.max_position_embeddings,
initializer_range=config.initializer_range,
rms_norm_eps=config.rms_norm_eps,
use_cache=config.use_cache,
rope_theta=config.rope_theta,
attention_bias=config.attention_bias,
attention_dropout=config.attention_dropout,
mlp_bias=config.mlp_bias,
)
self.model = LlamaModel(llama_config)
# Regression layer for multi-objective rewards
self.regression_layer = nn.Linear(config.hidden_size, config.num_objectives, bias=False)
# Gating network
self.gating = GatingNetwork(
config.hidden_size,
config.num_objectives,
hidden_dim=config.gating_hidden_dim,
num_layers=config.gating_num_layers,
temperature=config.temperature
)
# Reward transform matrix
self.reward_transform_matrix = nn.Parameter(
torch.eye(config.num_objectives), requires_grad=False
)
self.post_init()
def forward(self, input_ids=None, attention_mask=None, **kwargs):
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
hidden_states = outputs.last_hidden_state
device = hidden_states.device
# Last token pooling
if attention_mask is not None:
sequence_lengths = attention_mask.sum(dim=1) - 1
sequence_lengths = sequence_lengths.clamp(min=0).to(device)
batch_size = hidden_states.size(0)
batch_indices = torch.arange(batch_size, device=device)
pooled = hidden_states[batch_indices, sequence_lengths]
else:
pooled = hidden_states[:, -1, :]
# Multi-objective rewards (keep same dtype as pooled)
rewards = self.regression_layer(pooled)
# Gating weights
gate_weights = self.gating(pooled)
# Apply transform and compute final score (in float32 for stability)
# Ensure all tensors are on the same device
device = pooled.device
rewards_f32 = rewards.float()
gate_f32 = gate_weights.float()
transform_f32 = self.reward_transform_matrix.to(device).float()
coeffs = gate_f32 @ transform_f32.T
score = (rewards_f32 * coeffs).sum(dim=-1, keepdim=True)
return ArmoRMOutput(
logits=score.to(pooled.dtype),
score=score.to(pooled.dtype),
rewards=rewards,
gating_output=gate_weights,
)
|