Repository navigation
feat: 優化暴力搜索,使用 bitmask 向量化加速分析 - #7
Conversation
There was a problem hiding this comment.
Pull request overview
此 PR 將「大跌大漲」分析腳本中的暴力搜尋改為以 Python 大整數 bitmask 表示特徵命中集合,透過位元運算(AND / popcount)加速在訓練/驗證集上枚舉 2~3 條件組合並篩選候選規則。
Changes:
- 新增每日推文文字
_text_cache,避免重複 join/lower 以加速特徵計算。 - 以 bitmask 預先編碼 train/test 的特徵與目標標籤集合,將組合枚舉改為位元 AND + popcount。
- 將 2-combo 與 3-combo 搜尋改為巢狀迴圈搭配剪枝與統計輸出。
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # valid_mask = 所有有 next_td label 的日期 | ||
|
|
||
| def build_masks(dates, next_labels, feature_names, features_dict): | ||
| n = len(dates) |
| # --- 預計算單特徵的 matched mask(AND valid)--- | ||
| tr_single = [tr_feat_mask[useful[i]] & tr_valid for i in range(n_feat)] | ||
| te_single = [te_feat_mask[useful[i]] & te_valid for i in range(n_feat)] |
There was a problem hiding this comment.
@copilot apply changes based on this feedback
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot open a new pull request to apply changes based on the comments in this thread |
sstklen
left a comment
There was a problem hiding this comment.
@yongjer Welcome back! Two PRs in two days — you're on a roll! 🔥
Overview
Smart approach: bitmask encoding turns O(dates) per combo into a single & + popcount. This is the pure Python path — no GPU needed, no PyTorch dependency. Complements PR #6 nicely (GPU for massive batches, bitmask for quick local runs).
The _text_cache is a great find too — that repeated ' '.join(p['content'].lower() ...) was probably eating more time than anyone realized.
One thing to fix before merge
Result parity concern (also flagged by Copilot):
tr_single = [tr_feat_mask[useful[i]] & tr_valid for i in range(n_feat)]By ANDing with tr_valid here, dates without a next-trading-day label are excluded from train_total. In the original loop, those dates counted toward total but never toward hits — so the hit rate denominator was larger.
Example: if 10 dates match a feature combo, but 2 have no next-td label:
- Original: total=10, hits=6 → 60%
- Bitmask: total=8, hits=6 → 75% ← inflated
This isn't just cosmetic — it changes which rules survive the 50%/40% thresholds. We learned this lesson the hard way in PR #6 (the < 10 vs >= 5 saga 😄).
Fix options:
- Don't AND with
validintr_single— computetr_totalfromtr_matchdirectly (without valid), then computetr_hitsfromtr_match & tr_valid & tr_target[target] - Or: AND with valid (current behavior) but document it as an intentional change ("exclude unlabeled dates from denominator") and verify result parity
I'd suggest option 1 for exact parity, then we can decide if option 2 is actually better.
Quick wins (from Copilot, agree with all)
| # | What | Fix |
|---|---|---|
| 1 | popcount = int.bit_count |
✅ Saw you already asked Copilot to apply this |
| 2 | Remove unused n = len(dates) |
One-liner cleanup |
| 3 | Flip build_masks inner loop to iterate feat.keys() instead of all features |
Nice micro-optimization |
Benchmark ask
Once the parity fix is in, could you share before/after timing? Curious how bitmask compares to:
- Original CPU loop (~60s)
- Your PyTorch GPU version (0.07s on RTX 4060 Ti)
My guess: bitmask lands somewhere in the 1-5s range? That would make it the best "no dependencies" option.
Looking forward to the fix! 🚀
— TK
…sk generation logic for clarity and performance.
|
@sstklen The latest version should fix, but just in case, could u check the parity for me? The performance benchmarks will do it asap |
On m1 mac, the result as below, io bound. cuz it's not with gpu, I don't think the number on gpu will differ |
|
@yongjer Parity test done! ✅
The Performance summary across all three versions:
The bitmask version is the best "zero dependency" option — 240x faster with pure Python. Amazing work! 🚀 Merging now. — TK |
|
@yongjer Curious — what was your experience running the brute-force analysis locally? A few things I'd love to hear:
Two PRs in two days, each attacking the same problem from a completely different angle (GPU vs bitmask). That's rare. 🙏 — TK |
@sstklen I have zero experience, but with AI, nothing is impossible.
By the way, if there's an ELON CODE, it might be necessary to fully optimise the algorithm to save money on the VPS. |
feat: 優化暴力搜索,使用 bitmask 向量化加速分析
No description provided.