aquiro1994 commited on
Commit
290a4fd
·
verified ·
1 Parent(s): 4e7de42

Add Apple Silicon (MPS) usage section

Browse files

PyTorch does not auto-select mps, so Mac users silently run on CPU at ~4 rows/s instead of 177. Documents the device+dtype setup, length-sorted batching, measured throughput on an M5 Max, and why fp16 is safe above the 0.8 confidence threshold.

Files changed (1) hide show
  1. README.md +55 -0
README.md CHANGED
@@ -99,6 +99,61 @@ id2label = model.config.id2label
99
  print(f"Predicted NAICS: {id2label[predicted_class]}") # 62 (Health Care)
100
  ```
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  ## Input Format
103
 
104
  The model expects text in this format:
 
99
  print(f"Predicted NAICS: {id2label[predicted_class]}") # 62 (Health Care)
100
  ```
101
 
102
+ ## Running on Apple Silicon (Mac)
103
+
104
+ The model runs on the Mac GPU through Metal (`mps`). PyTorch does not select it
105
+ automatically, so pass the device explicitly — otherwise inference falls back to
106
+ CPU and is ~40x slower.
107
+
108
+ ```python
109
+ import torch
110
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
111
+
112
+ device = "mps" if torch.backends.mps.is_available() else "cpu"
113
+ dtype = torch.float16 if device == "mps" else torch.float32
114
+
115
+ model = AutoModelForSequenceClassification.from_pretrained(
116
+ "aquiro1994/naics-github-classifier", dtype=dtype
117
+ ).to(device).eval()
118
+ tokenizer = AutoTokenizer.from_pretrained("aquiro1994/naics-github-classifier")
119
+
120
+ def classify(texts, batch_size=32):
121
+ # Sort by length so each batch pads to a short common length
122
+ order = sorted(range(len(texts)), key=lambda i: -len(texts[i]))
123
+ out = [None] * len(texts)
124
+ for i in range(0, len(order), batch_size):
125
+ idx = order[i:i + batch_size]
126
+ batch = tokenizer([texts[j] for j in idx], padding=True, truncation=True,
127
+ max_length=512, return_tensors="pt").to(device)
128
+ with torch.no_grad():
129
+ # softmax in fp32: fp16 loses precision on near-uniform logits
130
+ probs = torch.softmax(model(**batch).logits.float(), dim=-1)
131
+ conf, pred = probs.max(dim=-1)
132
+ for k, j in enumerate(idx):
133
+ out[j] = (model.config.id2label[int(pred[k])], float(conf[k]))
134
+ return out
135
+ ```
136
+
137
+ Throughput on an Apple M5 Max (batch 32, 512 tokens):
138
+
139
+ | Device | Precision | rows/s |
140
+ |--------|-----------|-------:|
141
+ | CPU | fp32 | 4.3 |
142
+ | MPS | fp32 | 47.5 |
143
+ | **MPS** | **fp16** | **177** |
144
+
145
+ Notes:
146
+
147
+ - **fp16 is safe here.** On a 2,000-repo sample, labels above the `0.8` confidence
148
+ threshold matched fp32 **100%** of the time. Disagreements appear only below
149
+ `score < 0.4`, on inputs such as `Repository: ajax | README: \n`, where the model
150
+ spreads probability almost uniformly over the 19 classes and any numerical noise
151
+ flips the argmax.
152
+ - **Batch size 32-64 is the sweet spot**; larger batches are *slower*, not faster.
153
+ Peak memory was 6.5 GB.
154
+ - Sorting by length before batching is worth 2-5x on mixed-length inputs, because
155
+ otherwise every batch pads to its longest member.
156
+
157
  ## Input Format
158
 
159
  The model expects text in this format: