5.3.17 · D4 · HinglishMLOps & Deployment

ExercisesEdge deployment and ONNX

2,130 words10 min read↑ Read in English

5.3.17 · D4 · AI-ML › MLOps & Deployment › Edge deployment and ONNX

Shuru karne se pehle, ek symbol jo hum baar baar use karte hain:


L1 — Recognition

Q1.1

Har pain ko match karo — yeh edge se solve hoga ya cloud se: (a) raw camera frames kabhi device se bahar nahi jaane chahiye, (b) model ko 40 GB GPU memory chahiye, (c) robot ko 5 ms mein react karna hai aur Wi-Fi bhi nahi hai.

Recall Solution

(a) Edge — data device se bahar na jaana privacy solve karta hai. (b) Cloud — ek chhota edge chip 40 GB model hold nahi kar sakta; sirf cloud GPUs kar sakte hain. (c) Edge — 5 ms bina network ke matlab round-trip terms exist hi nahi karte; locally compute karna padega.

Q1.2

Woh chaar cheezein batao jo ONNX apne graph file mein store karta hai.

Recall Solution

nodes (operators jaise Conv, MatMul, Relu), initializers (the weights), typed inputs, aur typed outputs. Parent se mnemonic: N-O-R-I = Nodes, Operators, Runtime-Inputs.

Q1.3

providers=["CPUExecutionProvider"] mein, ek sentence mein batao ki Execution Provider (EP) kya hota hai?

Recall Solution

Ek EP woh backend hai jo actually graph ke operations compute karta hai — woh swappable engine (CPU, CUDA, TensorRT, CoreML, NNAPI) jise ONNX Runtime har operator deta hai. Related: TensorRT and GPU Optimization, Mobile ML - CoreML and TFLite.


L2 — Application

Q2.1

Ek model mein weights hain jo float32 mein stored hain. Yeh kitne megabytes (1 MB bytes) occupy karta hai? Int8 quantization ke baad?

Recall Solution

float32 = 4 bytes/weight: bytes MB. int8 = 1 byte/weight: bytes MB. Exactly shrink — parent ka "80/20 win".

Q2.2

Weights mein hain. Target signed int8 . Scale compute karo.

Recall Solution

Yeh formula kyun: yeh 10-wide real range ko 255-wide integer range mein fit karne ke liye stretch karta hai taaki dono endpoints line up ho jaayein.

Q2.3

Q2.2 ka use karke zero-point nikalo (nearest integer tak round karo).

Recall Solution

Kyun: woh integer hai jo exactly real tak decode hota hai. Kyunki range asymmetric hai, zero nahi hai — yeh crowded side ki taraf slide karta hai.

Q2.4

ko se quantize karo. Phir dequantize karo aur error batao.

Recall Solution

Error . ✓ theoretical bound ke andar hai.


L3 — Analysis

Q3.1

Weights ke liye mein do quantization schemes: A — ek global range . B — per-channel; busy channel hai. Busy channel ke andar ek value ke liye worst-case rounding error compare karo. Per-channel kyun jeetta hai?

Recall Solution

Scheme A: , toh . Scheme B: , toh . Per-channel error 10× chhoti hai ( vs ). Kyun: busy channel ki true values sirf span karti hain; global range apne 255 integer steps ka zyaadaatar empty space par waste karta hai, jisse har step coarse ho jaata hai. Tighter range ⇒ chhota ⇒ chhota error. Dekho Model Quantization and Pruning.

Q3.2

Cloud path: ms, ms, ms, ms. Edge inference ms. Kaun jeetta hai, aur kitne se?

Figure — Edge deployment and ONNX
Recall Solution

Edge jeetta hai ms se. Parent ki rearranged inequality check karo — compute penalty ms; network+queue ms. Kyunki , edge jeetta hai. ✓ Dekho Model Serving and Inference Latency.

Q3.3

Usi edge chip mein firmware bug aa jaata hai aur ab ms ho jaata hai. Recompute karo. Kya flip hua aur kyun?

Recall Solution

ms vs ms. Cloud ab jeetta hai ms se. Compute penalty ms ab network+queue ms se zyaada ho gayi, isliye inequality flip ho gayi. Lesson: winner "edge vs cloud" ki property nahi hai — yeh numbers par depend karta hai — hamesha inequality evaluate karo.


L4 — Synthesis

Q4.1

Aap ek PyTorch classifier export kar rahe ho jo (i) production mein variable batch sizes leta hai aur (ii) ek aise operator use karta hai jo sirf opset 16+ mein defined hai. Woh teen torch.onnx.export arguments likho jo matter karte hain aur har ek justify karo.

Recall Solution
torch.onnx.export(model, dummy, "m.onnx",
    input_names=["img"], output_names=["logits"],
    dynamic_axes={"img": {0: "batch"}},  # variable batch → axis 0 dynamic mark karo
    opset_version=16)                     # lowest opset jo woh op support karta hai
  • dummy (real-shaped tensor): export actual ops ko trace karta hai; koi input nahi ⇒ koi trace nahi ⇒ koi graph nahi.
  • dynamic_axes: iske bina, batch dummy ke size par freeze ho jaata hai aur production feeds shape-mismatch errors dete hain.
  • opset_version=16: lowest opset choose karo jo phir bhi tumhara op support kare, taaki runtimes ka widest set ise load kar sake. Zaroorat se zyaada high opset "unsupported opset" load failures ka risk deta hai.

Q4.2

Cloud output aur edge output mein max element ka difference hai. Tum np.allclose(a, b, atol=1e-4) call karte ho. Kya yeh pass hoga? Kya tumhe mismatch "fix" karna chahiye? Execution Providers ke terms mein explain karo.

Recall Solution

, isliye allclose True return karta hai — yeh pass hota hai. Tumhe bit-identical output force karne ki koshish nahi karni chahiye: alag EPs alag kernels, operator fusion, aur floating-point summation order use karte hain, isliye results sirf ek tolerance tak agree karte hain, exactly kabhi nahi. Agreement validate karna (equality nahi) sahi engineering check hai.


L5 — Mastery

Q5.1

Ek decision rule design karo — ek inequality mein — yeh decide karne ke liye ki edge par deploy karein ya nahi, given data size bytes, bandwidth bytes/s (uplink+downlink combined), queue , aur do inference times. Phir apply karo: B, B/s, s, s, s.

Recall Solution

Transfer time (bytes over bytes-per-second). Edge par deploy karo jab: Left = chhote chip ki compute penalty; right = network + queue cost jo bachti hai. Plug in: penalty s. Network+queue s. Kyunki edge par deploy karo (har call mein s bachta hai). Data Privacy and Federated Learning se ties karta hai jab privacy bhi round-trip rok deti hai.

Q5.2

value ke liye range mein int8 mein ek poora quantize→dequantize round-trip banao, phir exact reconstruction error batao. (L2 ka reuse karo.)

Recall Solution

, (Q2.2–Q2.3 se). Clamp check: ✓ (koi saturation nahi). Error . ✓ Round-trip guaranteed error box ke andar land karta hai.

Q5.3

Ek value ko usi scheme se quantize kiya jaata hai. Dikhao kya tootta hai aur standard fix ka naam batao.

Recall Solution

. Lekin int8 max hai, isliye ko clamp (saturate) karke 127 karna padega. Dequantized: — true crush hokar ban jaata hai, ka bada error. Kyun: calibrated range se bahar hai. Fix: range ko ek calibration dataset se choose karo jo aisi outlier values actually cover kare, ya quantization-aware training (QAT) use karo taaki network out-of-range activations se bachna seekhe. Dekho Model Quantization and Pruning.


Connections