24An image pipeline, then make it parallel
24 An image pipeline, then make it parallel
Anthropic candidates report an image round: given images and a list of functions, apply them in
order (rotate, grayscale and the like), with the interviewer adding a layer at each level
(1 report). One used PIL for grayscale, scale and resize, moved from small images to large ones,
and sped it up with ProcessPoolExecutor (1 report). Another did two parts, the transforms and
then the running time, and was asked why processes rather than threads and how many workers the
pool needs (1 report). A staff candidate was dropped into a project of real image files and had to
look up the library on the spot (1 report). Two more name only the topic, one as a transform
pipeline with scaling (2 sources). This is practice in that shape, not a leaked question.
Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/24_image_pipeline -k level1. Earlier levels must keep passing.
Images here are plain lists, so no image library is needed. An image is a list of rows, each row a
list of (r, g, b) tuples with values from 0 to 255. Every row has the same length, and every image
has at least one pixel. A step is a tuple: a name, then its arguments.
Level 1. apply(image, steps) runs the steps in order and returns a new image.
("grayscale",): each pixel becomes(y, y, y)withy = (299 * r + 587 * g + 114 * b) // 1000.("rotate", degrees): 90, 180 or 270 degrees, clockwise.("resize", width, height): nearest neighbor. WithWandHthe input's width and height,out[y][x] = image[y * H // height][x * W // width].("crop", left, top, width, height): thewidthbyheightblock whose top left pixel isimage[top][left]. A crop that doesn't fit inside the image at that point in the pipeline raisesValueError.("map", func): every pixelpbecomesfunc(p).
apply checks every step before it runs any. An unknown name, the wrong number of arguments, a
rotation other than 90, 180 or 270, a resize or crop size below 1, a negative crop corner, or a
map without a callable raises ValueError. The input image is never changed, and the image you
return shares no rows with it, even when there are no steps.
Level 2. process_batch(images, steps) returns one result per image, in input order: the new
image, or the exception that stopped that image. One bad image (a crop too big for it, a map that
raises) doesn't stop the rest. A bad step list is different: it raises ValueError before any image
runs, even when images is empty.
Level 3. process_batch(images, steps, workers) runs up to workers images at a time on
threads (concurrent.futures.ThreadPoolExecutor). Results still come back in input order, and a
failure still stays with its image. The tests watch from inside a map step: they hold images
until workers of them run at once, and they count how many ever run together. If your workers
never overlap, a test waits 5 seconds and then fails. If more than workers run at once, a test fails
too.
Threads, because the tests can only watch workers inside their own process. That's also the follow-up: these pixel loops are pure Python, so the GIL lets one thread run at a time and threads won't make them faster. Be ready to say what would (a process pool, and why a step list of plain tuples can be sent to another process while a lambda can't, or a library whose C code releases the GIL) and how many workers you'd start for CPU-bound work and for I/O-bound work.
Level 4. One huge image, where splitting the work by image doesn't help.
apply_in_bands(image, steps, workers, band_rows) returns exactly what apply(image, steps)
returns, but cuts the image into bands of band_rows rows (the last band can be shorter) and works
on up to workers bands at a time. grayscale and map go pixel by pixel, so they can run on
bands. rotate, resize and crop need the whole image. Run each stretch of consecutive
pixel-by-pixel steps over the bands in parallel, the other steps on the whole image between them,
and stitch the bands back in order. Check every step before anything runs, as apply does.