unsloth/tests/python
Roland Tannous 673b7f86ba Studio: late chunking opt-in per KB (Phase 3B-late)
When a KB has chunking_strategy = 'late', ingestion takes a separate
code path that embeds the full document in a single forward pass and
mean-pools token embeddings per chunk span. Each chunk vector carries
full-document context via the encoder's bidirectional attention —
Jina's published technique, ~+6.5 nDCG@10 on long docs.

Backend
- chunking.py: new chunk_pages_with_spans() that joins all pages into a
  single full_doc, runs the existing recursive splitter, and returns
  per-chunk (char_start, char_end) offsets. Page-number metadata is
  recovered by overlap with the original page ranges so PDF citations
  still work. Existing chunk_pages() unchanged.
- embeddings.py: new late_chunk_encode(doc_text, char_spans). Tokenizes
  the doc with return_offsets_mapping, runs the underlying transformer
  to get per-token last_hidden_state, then mean-pools per chunk span.
  When the doc exceeds the embedder's context, falls back to windowed
  late chunking with a 512-token overlap so cross-window context is
  partially preserved.
- ingestion.py _subprocess_worker: branches on chunking_strategy.
  'late' path: chunk_pages_with_spans -> late_chunk_encode -> one big
  chunks_batch message. 'standard' path unchanged. Both reuse the same
  parent-side pump.
- ingestion.enqueue_ingestion: new chunking_strategy + mode kwargs;
  defaults to 'standard' / 'text' for legacy callers. embedder model
  resolved via resolve_embedder() from the (mode, strategy) matrix.
- routes/rag.py: KB-doc upload reads chunking_strategy + mode from the
  KB row (defensive .get for pre-Phase-3 schemas) and threads them
  through _start_ingestion.

Frontend
- kb-create-dialog.tsx: new "Chunking strategy" select with Standard /
  Late options. Embedding-model placeholder switches to nomic when
  Late is picked. createKB request now carries chunking_strategy.
- kb-list.tsx + chat-settings-sheet.tsx: small " Late" badge next to
  late-chunking KB names in the settings KB list and the chat sidebar
  dropdown so users see the mode at a glance.

Tests
- test_rag_late_chunking.py: pure-python tests for chunk_pages_with_spans
  (chunks index back into full_doc; page numbers inherited by overlap;
  pages joined with blank line). A server-marked test loads
  all-MiniLM-L6-v2 to exercise late_chunk_encode end-to-end.

No multimodal yet; that's Phase 3B-multimodal (next PR).
2026-05-24 12:18:09 +04:00
..
__init__.py Consolidate dual venvs and separate install from update (#4530) 2026-03-25 05:24:21 -07:00
conftest.py fix: add tokenizers to no-torch deps and TORCH_CONSTRAINT for arm64 macOS py313+ (#4748) 2026-04-01 06:12:17 -07:00
test_cross_platform_parity.py Add configurable PyTorch mirror via UNSLOTH_PYTORCH_MIRROR env var (#5024) 2026-04-15 11:39:11 +04:00
test_dpo_vision_processor_passthrough.py Fix DPO trainer multi process hang (#5199) 2026-04-29 04:15:34 -07:00
test_e2e_no_torch_sandbox.py tests: add no-torch / Intel Mac test suite (#4646) 2026-03-27 02:33:45 -07:00
test_fast_sentence_transformer_redirect_lifecycle.py Add Studio PR-time CI: pin enforcement, frontend, backend, wheel smoke (#5298) 2026-05-06 04:41:57 -07:00
test_flash_attn_install_python_stack.py studio: skip flash-attn install on Blackwell GPUs (sm_100+) (#5420) 2026-05-14 18:13:50 +04:00
test_gpu_init_ldconfig_guard.py feat(studio): MLX training tab on Apple Silicon (LoRA / full FT, VLM, export) (#5265) 2026-05-05 23:54:58 -07:00
test_install_python_stack.py Consolidate dual venvs and separate install from update (#4530) 2026-03-25 05:24:21 -07:00
test_no_torch_filtering.py [Studio] Install flash attn at setup time for linux (#4979) 2026-04-14 16:40:17 +04:00
test_orpo_processor_text_tokenizer.py Fix ORPO text-only tokenization with processors (#5501) 2026-05-18 00:40:30 -07:00
test_patch_trl_rl_trainers_defensive.py fix: unblock 4 tests deselected/skipped in #5312 (real bugs) (#5359) 2026-05-11 02:39:17 -07:00
test_rag_bm25.py Studio: add RAG with hybrid search, reranker, chat integration 2026-05-23 18:46:15 +04:00
test_rag_chunking.py Studio: layout-aware RAG parsers + heading-aware chunking (Phase 3A) 2026-05-24 11:10:13 +04:00
test_rag_late_chunking.py Studio: late chunking opt-in per KB (Phase 3B-late) 2026-05-24 12:18:09 +04:00
test_rag_parsers.py Studio: layout-aware RAG parsers + heading-aware chunking (Phase 3A) 2026-05-24 11:10:13 +04:00
test_rag_reranker.py Studio: add RAG with hybrid search, reranker, chat integration 2026-05-23 18:46:15 +04:00
test_rag_retrieval.py Studio: add RAG with hybrid search, reranker, chat integration 2026-05-23 18:46:15 +04:00
test_rag_vector_store.py Studio: add RAG with hybrid search, reranker, chat integration 2026-05-23 18:46:15 +04:00
test_studio_import_no_torch.py tests: add no-torch / Intel Mac test suite (#4646) 2026-03-27 02:33:45 -07:00
test_tokenizers_and_torch_constraint.py fix: add tokenizers to no-torch deps and TORCH_CONSTRAINT for arm64 macOS py313+ (#4748) 2026-04-01 06:12:17 -07:00
test_unsloth_run_tool_policy_resolver.py unsloth run: add --enable-tools/--disable-tools server-side tool policy (#5277) 2026-05-05 12:45:15 +04:00