Process-based concurrency
Launch isolated Python workers with the spawn context and route batches through a bounded shared task queue.
A professional-grade Python inference workflow built around PyTorch, Qwen2.5-0.5B-Instruct and Hugging Face Transformers. Distribute JSONL review-classification batches across independent CPU or GPU workers, track execution and restore every result to its original input position.
The checked-in Python implementation covers orchestration, device-aware generation, record validation and run instrumentation. These capabilities are available as a standalone CLI with a companion Jupyter notebook.
Launch isolated Python workers with the spawn context and route batches through a bounded shared task queue.
Load Qwen/Qwen2.5-0.5B-Instruct using Transformers, with chat-template prompts and greedy text generation.
Assign one full model replica per selected CUDA GPU. CPU inference and an offline heuristic mode are also supported.
Workers consume available batches as they become ready, using a common producer queue rather than fixed file partitions.
Group reviews by approximate text length before tokenization to reduce unnecessary padding in each mini-batch.
Validate each input object, required text field and nonempty review before scheduling inference work.
Track original row indices across asynchronous workers, then write completed predictions in input order.
Preserve the raw generated output and separately parse positive or negative labels, leaving unrecognized outputs null.
Report model loading time, inference time, rows per second, output tokens per second and per-worker records.
Check worker exit codes, reject duplicate responses, enforce timeouts and terminate remaining processes on failure.
Run multiple CPU workers without downloading a model to exercise queue distribution and file processing.
Use the self-contained Colab/Kaggle notebook, which launches the multiprocessing CLI through subprocesses.
The parent process owns validation, scheduling and result assembly. Each worker owns model initialization and batch generation on its assigned device. All model replicas remain independent.
Load review records from JSONL, or generate synthetic examples. Reject invalid or empty review text.
Sort original row indices by review length and place groups into a queue, retaining the record-to-index mapping.
Spawn processes with a device assignment. Model loading is serialized to reduce simultaneous memory and download spikes.
Each worker produces a classification string and returns its batch output. The coordinator validates batch completeness.
Write outputs in the original order and report throughput, initialization timing and worker-level counts.
DistributedDataParallel training, tensor parallelism, or pooled GPU memory. Every selected GPU must fit the complete model and its inference buffers.Start with the offline process test to verify the data path, then select real Qwen inference when the model weights and supported hardware are available.
Runs the actual multiprocessing and JSONL workflow with a deterministic keyword-based sentiment heuristic. It does not initialize an LLM and is suitable for validating orchestration independently of model downloads.
cpu worker slotsLoads Qwen2.5-0.5B-Instruct as a causal language model on each selected device. A constrained prompt requests a single sentiment label; output is normalized only if it matches the expected format.
| Property | CPU mock | Qwen inference |
|---|---|---|
| Model loaded | No; deterministic heuristic | Qwen2.5-0.5B-Instruct per device |
| Devices | cpu cpu, etc. | cuda:0, cuda:0 cuda:1, or CPU |
| Batch size default | 2 | 2 (often reduce to 1 on limited VRAM) |
| Max input tokens | Not used by heuristic | 256 default; configurable |
| Max new tokens | Not used by heuristic | 8 default; configurable |
| Prediction output | Positive / negative heuristic | Decoded LLM text with strict label normalization |
| Validation purpose | Queue and file handling | Model inference behavior on selected runtime |
Each input row must be a JSON object containing a nonempty review string, unless another field is selected with --text-field. Existing metadata travels through the pipeline unchanged.
The repository includes 48 synthetic, labeled product-review rows. Labels are optional; when provided, they can be used for a simple accuracy calculation.
{"id":"R001","product":"smartphone","review":"The display is excellent and works perfectly.","label":"positive"}
{"id":"R002","product":"SSD","review":"The product stopped working.","label":"negative"}Every record retains its original fields and adds the model's raw llm_output and a strictly normalized predicted_label. An unexpected response produces null for that label.
{"id":"R001","review":"The display is excellent.","label":"positive","llm_output":"positive","predicted_label":"positive"}
{"id":"R002","review":"The product stopped working.","label":"negative","llm_output":"negative","predicted_label":"negative"}predicted_label is null if the generated response is not a recognized one-word sentiment label.Commands below use filenames verified in the current GitHub repository. The project works as a standalone Python script; the notebook launches that script in subprocesses to accommodate multiprocessing.
git clone https://github.com/su394611-del/Parallel-LLM-Inference.git
cd Parallel-LLM-Inference
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install --upgrade pip
.\.venv\Scripts\python.exe -m pip install -r requirements.txt.\.venv\Scripts\python.exe parallel_llm_inference.py --mock --devices cpu cpu --input reviews.jsonl --output results_mock.jsonl --batch-size 4.\.venv\Scripts\python.exe -c "import torch; print('CUDA:',torch.cuda.is_available()); print('GPU count:',torch.cuda.device_count()); print('PyTorch CUDA:',torch.version.cuda)".\.venv\Scripts\python.exe parallel_llm_inference.py --devices cuda:0 --input reviews.jsonl --output results_gpu.jsonl --batch-size 1 --max-input-tokens 192 --max-new-tokens 8.\.venv\Scripts\python.exe parallel_llm_inference.py --devices cuda:0 cuda:1 --input reviews.jsonl --output results_2gpu.jsonl --batch-size 2 --max-input-tokens 192 --max-new-tokens 8The code implements queue-based multiprocessing, offline mock processing and a Qwen inference path. Mock results do not establish actual LLM accuracy or GPU scaling, and no measured two-GPU speedup is claimed. This project is a batch CLI workflow, not a persistent HTTP serving system or model-parallel inference engine.
Links below point to repository files verified on the current legacy default branch. The separate GitHub Wiki is linked as a documentation resource.