Models · Edges and lines

M-LSD line segments

M-LSD (NAVER, AAAI 2022): finds the straight line segments of a photo (rooms, buildings) with a small MobileNetV2 network and draws them as a 1-pixel line map for the MLSD ControlNet; returns the segments too. Apache-2.0 code and weights.

experimental Fork of huggingface/controlnet_aux@b1aac31 · backends: portable, kernels

Checkpoint mlsd

Paper 2021 Added in 0.2.0 Evaluated with 0.2.0

Portable, RTX 5060 Ti 16 GB 9.33 ms 1080x1920, fp16
vs the faster original 3.8× original fp16: 35.6 ms
Peak memory 127 MiB original: 185 MiB
Accuracy vs original fp32 – original not run on the test set

Use it

pip install fastforks

Lean install with only what this model needs: pip install fastforks-core

from fastforks import edges

model = edges.load("mlsd")  # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.edges  # also: lines
result.backend  # what ran: mode, device, dtype, kernels
fastforks bench mlsd

Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.

Speed and memory

Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.

BackenddtypeTime per call (ms)vs original fp16Peak memory
allocated / reserved
Load (s)First call (s)
Original fp32fp32not measured ––––
Original fp16torch.autocast(float16) around the call; huggingface/controlnet_aux@b1aac318b15c mlsd, host OpenCV resize, float post-processingfp16 35.6–185 MiB/212 MiB0.10.30
Portable fp32fp32not measured ––––
Portablefp16 9.333.8×127 MiB/170 MiB0.20.01
Kernelsnot measured ––––
Portable + torch.compilenot measured here ––––
TensorRTnot measured here ––––

Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-14. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.

Other devices

DeviceBackenddtypems / callPeak memoryOutput vs referenceInput
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp3255.3 1,500 MiB process–1024x1024
Portablefp1644.6 1,373 MiB processmatches1024x1024
Portablefp3250.0 1,371 MiB processmatches1024x1024
Kernelsfp32not implemented–1024x1024

CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.

Licence and weights

Commercial use allowed

Code
Apache-2.0 LICENSE
Weights
Apache-2.0 source
Training data
Not stated for this checkpoint; the M-LSD paper trains on the Wireframe dataset (Huang et al. 2018, no licence stated).

Our reading of the licences, not legal advice.

Weights

mlsd_large_512_fp32.pth 6.3 MB· hosted on raw.githubusercontent.com

fastforks downloads and checks these files on first use (fastforks download mlsd).

Accuracy

The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.

YorkUrban test (102 images)

line segment sAP@5 / @10 / @15 %

Rundtypesap5 ↑sap10 ↑sap15 ↑Δ sap5 vs fp32 originalimagesms / image
Original fp32fp326.6913.4417.01 reference 102 18.3
Original fp16not measured
Portable fp32fp326.6913.4417.02 +0.00 102 95.9
Portablenot measured
Kernelsnot measured

All 102 YorkUrban images in the DT-LSD release. Each M-LSD mode uses 512-pixel short-side detection, cubic resizing, a score and length threshold of 0.1, and up to 200 segments. Structural AP matches endpoints on a 256 x 256 coordinate grid, testing both endpoint orders and matching each annotation at most once. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-18; portable fp32: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 3bf0bac7, 2026-11-18.

Closeness to the original

Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output edges).

CheckToleranceMeasured
CPU fp32, max |diff|≤ 0.002 not recorded
cpu_outliers≤ 0.001 not recorded
GPU default dtypes, mean |diff|≤ 0.02 0
GPU default dtypes, p99 |diff|≤ 0.25 0

Recorded for mlsd on RTX 5060 Ti, torch 2.10.0+cu130, 2026-11-17, with these kernels: cudnn_hacccutlass_convigev

Details

Mobile line segment detector (Gu et al., AAAI 2022; controlnet_aux `MLSDdetector`, ComfyUI's "M-LSD Lines"): a MobileNetV2 network that predicts segment centers and end-point displacements; returns the straight segments and the 1-pixel line map ControlNet's MLSD model takes.

Input / output
Input: one RGB image. Output: a line / edge map [H, W] in [0, 1] at the input size (1 = line), the ControlNet preprocessors (line art, HED, PiDiNet, TEED / Anyline); M-LSD also returns straight line segments.
Input size
any H x W >= 32, padded to a multiple of 32; tested up to 1080x1920 (127 MiB allocated)
Options
  • short_side=512>= 64· changes the output
  • thr_v=0.10.0 to 1.0· changes the output
  • thr_d=0.1>= 0.0· changes the output
Outputs
edges, lines
CUDA kernels
  • igevIGEV stereo: convolution epilogues, correlation lookups, GRU gates
  • cutlass_convCUTLASS convolutions with the recurrent update (GRU) fused in
  • cudnn_haccconvolutions with fp16 accumulation (cuDNN)
Apple (MPS)
fp32, fp16
Full reference, generated from the repository

M-LSD (NAVER, AAAI 2022): finds the straight line segments of a photo (rooms, buildings) with a small MobileNetV2 network and draws them as a 1-pixel line map for the MLSD ControlNet; returns the segments too. Apache-2.0 code and weights.

Commercial use: yes.

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
mlsdsynthetic (1080x1920)original
portable
kernels
35.6
9.33
–
185 / 212
127 / 170
–
mlsdsynthetic (1024x1024)original
portable
kernels
portable + torch.compile
TensorRT fp16
20.6
7.65
–
4.61
planned for 0.2.1
113 / 132
72 / 90
–
101 / 130
–
Details
  • Original code: https://github.com/huggingface/controlnet_aux at b1aac318b15c
  • Code licence: Apache-2.0 (permissive); files: LICENSE
  • Weights licence: Apache-2.0
  • Licence evidence: https://github.com/lhwcv/mlsd_pytorch/blob/2312205254e66911703decf775f626995d260f17/LICENSE ; https://github.com/lhwcv/mlsd_pytorch/tree/2312205254e66911703decf775f626995d260f17/models
  • Training data: Not stated for this checkpoint; the M-LSD paper trains on the Wireframe dataset (Huang et al. 2018, no licence stated).
  • Options: short_side = 512 (>= 64; also resolution; also detect_resolution), thr_v = 0.1 (0.0 to 1.0), thr_d = 0.1 (>= 0.0)
  • Kernel libraries: igev, cutlass_conv, cudnn_hacc
  • Variants: mlsd
  • Not in this release (planned for 0.2.1): mlsd TensorRT export: TensorRT builds the engine but its CNN outputs diverge from portable FP32 on real images
  • Outputs: edges, lines
  • Input size: any H x W >= 32, padded to a multiple of 32
  • Tested up to (portable, fp16, RTX 5060 Ti 16 GB): mlsd 1080x1920 (0.1 GiB peak allocated, 0.2 reserved)
  • mlsd 1024x1024, first call: portable + torch.compile 11 s (cold Inductor cache).
  • mlsd original 1080x1920, original 1024x1024: torch.autocast(float16) around the call; huggingface/controlnet_aux@b1aac318b15c mlsd, host OpenCV resize, float post-processing
  • mlsd load / first call: original 0.21 s / 0.057 s, portable 0.17 s / 0.011 s
  • Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5

Measured

All 102 YorkUrban images in the DT-LSD release. Each M-LSD mode uses 512-pixel short-side detection, cubic resizing, a score and length threshold of 0.1, and up to 200 segments. Structural AP matches endpoints on a 256 x 256 coordinate grid, testing both endpoint orders and matching each annotation at most once. Original = the original code named under Details, same checkpoint and protocol.

variantdatasetoriginal fp32portable fp32note
mlsdYorkUrban test (102 images), line segment sAP@5 / @10 / @15 %6.69 / 13.44 / 17.016.69 / 13.44 / 17.02

Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 3bf0bac7, 2026-11-18.

Parity

CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output edges).

variantkernels error vs original, mean / p99GPU, torchkernels used
mlsd0.0 / 0.0RTX 5060 Ti, torch 2.10.0+cu130cudnn_hacc@cudnn91501, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120

Example output

No example rendered for this checkpoint yet.