Skip to main content

Featured

GPU Memory Sharding with FSDP

GPU Memory Sharding with FSDP

DISTRIBUTED DEEP LEARNING — PART 1/5 Day 1: Demystifying GPU Memory & The ZeRO Revolution — DeepSpeed, FSDP & State Sharding 25 min read Series: The Dharma of Development Distributed DL (Day 1 / 5) Level: Principal / Systems AI Engineer ðŸ’Ĩ Context: You attempt to fine-tune or pretrain a 13-billion parameter dense transformer on an 80GB NVIDIA H100 or A100 GPU using standard PyTorch. You set your batch size to 1. You press enter. Within three seconds, your terminal explodes with the most dreaded error in artificial intelligence: torch.cuda.OutOfMemoryError: CUDA out of memory . How is this possible? In 16-bit precision, 13 billion parameters occupy only ~26 GB of disk space. Why can't an 80 GB state-of-the-art GPU train a 26 GB model? Because the naive mental model of machine learning memory is deeply flawed. Today, we demystify the true $16\Phi$ memory footprint of neural networks and master the architecture that made modern large language models possi...

Python Production File Handling — aiofiles, mmap & Atomic Writes (2026)

Skip to main content

Day 10: The Akashic Records — Production File Handling & I/O

  • Series: Logic & Legacy
  • Day 10 / 30
  • Level: Senior Architecture

Prerequisite: We have shattered linear time in the Asynchronous Matrix. Now, we must learn to record our system's Karma permanently into the physical architecture of the disk.

To master Python file handling and reading large files in Python, we must abandon the illusions taught to beginners. We are no longer writing scripts; we are writing systems. We will bypass the standard blocking open(), utilize aiofiles and orjson for blinding speed, protect against data corruption with atomic swaps, and wield the Brahmastra of I/O: mmap.

System diagram showing file operation layers from the application level through buffered I/O to the OS kernel and physical disk.

1. The Akasha: The Maya of Synchronous open()

The built-in open() function is Maya (an illusion). It hides a complex CPython hierarchy: io.TextIOWrapper → BufferedWriter → io.FileIO. At the absolute bottom is the only reality the kernel cares about: an OS File Descriptor (FD).

2. The High-Performance Arsenal: aiofiles & orjson

To architect production-grade storage, we must equip our environment with high-performance, non-blocking alternatives.

Infrastructure Requirements
pip install aiofiles orjson cryptography aiosqlite
  • aiofiles: True non-blocking I/O for async event loops.
  • orjson: Rust-backed JSON parsing that operates on raw bytes for speed.
  • cryptography: Symmetric encryption to protect data at rest.

3. O(1) Streaming: Parsing Multi-Gigabyte Files

How do you read a 500GB server log with 16GB of RAM? If you use f.read(), you trigger the Out-Of-Memory (OOM) killer. Senior Architects use Generators to maintain a constant (O(1)) memory footprint.

The O(1) Async Memory Pipeline
import asyncio, aiofiles

async def stream_massive_logs(path):
    # The 'async for' pulls exactly one line from the disk buffer at a time.
    async with aiofiles.open(path, mode='r') as f:
        async for line in f:
            if "[CRITICAL]" in line:
                yield line.strip()

4. The Atomic Writ: Engineering Corruption-Free Saves

Executing open(file, 'w') directly on production data is a liability. It instantly truncates the file. If the system crashes mid-write, your data is gone. We use the Write-Rename Pattern.

The Atomic Write Implementation
import os, pathlib, aiofiles, orjson

async def atomic_save(target_path, data):
    tmp = pathlib.Path(target_path).with_suffix('.tmp')
    async with aiofiles.open(tmp, 'wb') as f:
        await f.write(orjson.dumps(data))
        await f.flush()
        os.fsync(f.fileno()) # Hard flush to hardware
    
    # Atomic swap of the metadata pointer
    os.replace(tmp, target_path)

5. The Brahmastra: Zero-Copy Memory Mapping (mmap)

Standard read() requires two memory copies (Disk → Kernel → App). mmap maps the file directly into the process's virtual address space. It is Zero-Copy power.

Operation (NVMe) Standard Python read() Memory-Mapped (mmap)
Sequential Read (10GB) 2,400 MB/s 9,800 MB/s
Random Access Latency 95 Ξs (Syscalls) 12 Ξs (Pointer Math)

6. Dharmic Governance: Custom Context Managers

In Python, Duty (Dharma) is enforced by the with statement. We can architect custom managers to handle the "Triple-Shadow" of exceptions (type, value, traceback).

Automated Resource Reclamation
class AtomicWriter:
    def __enter__(self):
        self.f = open(self.tmp, 'wb')
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.f.close()
        if exc_type is None:
            os.replace(self.tmp, self.target)
        else:
            os.remove(self.tmp) # Rollback on failure

🛠️ Day 10 Project: The Resilient Log Engine

  • Implement the AtomicWriter context manager to save a system_state.json file.
  • Intentionally raise an exception inside the with block.
  • Verify that the original file remains untouched and the temporary file is purged.
ðŸ”Ĩ PRO UPGRADE: MMAP MULTIPROCESSING

Your challenge: Use mmap.MAP_SHARED to map a single file. Spawn two separate Python processes using multiprocessing and have them communicate by reading/writing directly to the mapped memory segments. No sockets, no pipes—just raw hardware-speed IPC.

FAQ: High-Performance I/O

Why is f.flush() not enough to guarantee data safety?

f.flush() only moves data from Python's internal memory buffer to the Operating System's buffer. If the power fails, the OS buffer is lost. You must call os.fsync() to force the kernel to physically commit the bytes to the hard drive platters/cells.

Does mmap work on both Windows and Linux?

Yes, but the underlying kernel APIs differ. Linux uses mmap syscalls, while Windows uses "File Mapping" objects. Python's mmap module abstracts these differences, but you must be careful with flags like access=mmap.ACCESS_WRITE which have subtle platform specific behaviors.

Why is orjson better for production than the standard json?

Standard json is written in C but operates on high-level Python string objects. orjson is written in Rust and handles UTF-8 byte serialization natively. It is typically 5x to 10x faster and correctly handles dataclass and datetime objects without custom encoders.

Comments