modelscope / modelscope/ms-swift

Gemma3 SFT fails to preprocess a custom JSONL dataset (NoneType not iterable) and ends up with an empty train set

Open
#7,366 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
Python
Stars
15.7k
Forks
1.7k
Avg merge
1d 16h
Merged PRs (30d)
136

Description

Env:

Training setup: 8 GPUs
ms-swift: 3.9.1
deepspeed: 0.17.4
transformers:4.55.0

Description:
I’m fine-tuning Gemma3 Instruct using swift sft with a custom JSONL dataset.
With the same training command and the same dataset, switching only --model to Qwen3 works, but Gemma3 fails during dataset preprocessing / template encoding.

Error:
[WARNING:swift] 👆👆👆There are errors in the dataset, the data will be deleted
[INFO:swift] Traceback (most recent call last):
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/dataset/preprocessor/core.py", line 178, in batched_preprocess
row = self.preprocess(row)
^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/dataset/utils.py", line 289, in preprocess
encoded = self.template.encode(row, return_length=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/base.py", line 497, in encode
encoded = self._encode_truncated(chosen)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/base.py", line 1201, in _encode_truncated
encoded = self._encode(inputs)
^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/base.py", line 1229, in _encode
self._swift_encode(inputs) if template_backend == 'swift' else self._jinja_encode(inputs))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/template/gemma.py", line 90, in _swift_encode
return super()._swift_encode(inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/base.py", line 1085, in _swift_encode
self._concat_context_list(prefix, res_context_list, res_context_types, system=system)
File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/template/base.py", line 690, in _concat_context_list
for context in context_list:
TypeError: 'NoneType' object is not iterable

[rank0]: Traceback (most recent call last):
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/cli/sft.py", line 10, in
[rank0]: sft_main()
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/train/sft.py", line 340, in sft_main
[rank0]: return SwiftSft(args).main()
[rank0]: ^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/base.py", line 49, in main
[rank0]: result = self.run()
[rank0]: ^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/train/sft.py", line 169, in run
[rank0]: train_dataset, val_dataset = self._prepare_dataset()
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/train/sft.py", line 131, in _prepare_dataset
[rank0]: datasets = self._post_process_datasets(datasets)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/train/sft.py", line 164, in _post_process_datasets
[rank0]: self._show_dataset(*datasets)
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/swift/llm/train/sft.py", line 292, in _show_dataset
[rank0]: inputs = train_dataset[0] if hasattr(train_dataset, 'len') else next(iter(train_dataset))
[rank0]: ~~~~~~~~~~~~~^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/datasets/arrow_dataset.py", line 2780, in getitem
[rank0]: return self._getitem(key)
[rank0]: ^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/datasets/arrow_dataset.py", line 2764, in _getitem
[rank0]: pa_subtable = query_table(self._data, key, indices=self._indices)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/datasets/formatting/formatting.py", line 593, in query_table
[rank0]: _check_valid_index_key(key, size)
[rank0]: File "/home/tiger/.local/lib/python3.11/site-packages/datasets/formatting/formatting.py", line 533, in _check_valid_index_key
[rank0]: raise IndexError(f"Invalid key: {key} is out of bounds for size {size}")
[rank0]: IndexError: Invalid key: 0 is out of bounds for size 0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Gemma3 SFT run with the custom JSONL dataset, then inspect swift/llm/template/template/gemma.py and the encoding flow in swift/llm/template/base.py. Follow how preprocessing in swift/llm/dataset/preprocessor/core.py handles the failed row and how swift/llm/train/sft.py reports the dataset. Done means valid input no longer raises the NoneType error and the resulting train set is nonempty.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.