• PyTorch की आंतरिक संरचना का विवरण, जो उन लोगों के लिए एक गाइड है जो PyTorch के C++ codebase में योगदान करना चाहते हैं
  • इस लेख का लक्ष्य PyTorch की tensor library की संरचना और automatic differentiation (autograd) तकनीक को समझने में मदद करना है, साथ ही codebase में रास्ता खोजने में भी

PyTorch tensor की बुनियादी संरचना

  • PyTorch में tensor सबसे बुनियादी data structure है
  • tensor एक n-आयामी data structure है, जिसमें float, int जैसे scalar values संग्रहित किए जा सकते हैं
  • tensor में निम्न metadata शामिल होता है:
    • आकार (size): tensor के dimensions की जानकारी
    • dtype: संग्रहित data type (जैसे float32, int64 आदि)
    • device: data कहाँ संग्रहित है (CPU, CUDA आदि)
    • stride: physical memory में data के offset की जानकारी
  • Stride की भूमिका

    • stride का उपयोग logical index को physical memory location में बदलने के लिए किया जाता है
    • stride हर dimension के लिए offset सेट करता है और index पर stride value गुणा करके physical memory location निर्धारित करता है
    • stride की मदद से नया tensor बनाए बिना उसी data को view के रूप में अलग तरीके से देखा जा सकता है

tensor और Storage की अवधारणा

  • PyTorch में tensor वास्तविक data को सीधे संग्रहित नहीं करता → data को Storage में प्रबंधित किया जाता है
  • Tensor = size + dtype + device + stride + offset जानकारी
  • कई tensor एक ही Storage साझा कर सकते हैं → इससे View की अवधारणा समर्थित होती है
  • Storage और tensor को अलग रखने से memory का अधिक दक्ष उपयोग संभव होता है

tensor operations की dispatch प्रक्रिया

  • PyTorch में operations दो चरणों की dispatch से गुजरते हैं:
    1. device type और layout आधारित dispatch
      • CPU tensor बनाम CUDA tensor के अनुसार अलग implementation code चलता है
    2. dtype आधारित dispatch
      • float बनाम int जैसे data type के अनुसार अलग kernel कॉल होता है

PyTorch tensor extension model

  • tensor के तीन मुख्य extension तत्व:

    • Device: CPU, GPU, TPU आदि पर memory allocation का तरीका परिभाषित करता है
    • Layout: tensor memory में कैसे संग्रहित होगा, यह परिभाषित करता है (जैसे contiguous storage, sparse storage आदि)
    • dtype: tensor के प्रत्येक element में संग्रहित data type को परिभाषित करता है
  • extension विकल्प:

    • PyTorch code को सीधे संशोधित करके tensor को extend किया जा सकता है
    • मौजूदा tensor को wrap करने वाला wrapper class लिखा जा सकता है
    • अगर automatic differentiation के दौरान wrapper की ज़रूरत हो, तो direct extension की आवश्यकता होती है

automatic differentiation (Autograd) कैसे काम करता है

  • PyTorch reverse-mode differentiation के आधार पर automatic differentiation करता है
  • forward operation के समय graph बनता है → backward pass के समय graph को traverse करते हुए differentiation किया जाता है
  • Autograd निम्न अतिरिक्त जानकारी प्रबंधित करता है:
    • AutogradMeta: tensor से जुड़ा metadata, जिसका उपयोग backward pass में होता है
    • operation के result को रिकॉर्ड करना और backward pass के दौरान differentiation करना

PyTorch code structure और file locations

  • PyTorch codebase में मुख्य directories:
    • torch/ → Python module (Python code)
    • torch/csrc/ → Python और C++ binding code, automatic differentiation engine, JIT compiler आदि
    • aten/ → tensor operations की परिभाषाएँ (अधिकांश core operations शामिल)
    • c10/ → tensor और Storage जैसी core structures की परिभाषाएँ

PyTorch operation execution process

  • उदाहरण: torch.add() कॉल होने पर चलने वाली प्रक्रिया:
    1. Python से C++ code में arguments का conversion
    2. VariableType में dispatch
    3. Device/layout आधारित dispatch
    4. अंतिम kernel execution

kernel लिखने की प्रक्रिया और tools

  • PyTorch में kernel निम्न चरणों में लिखा जाता है:
    1. operation metadata लिखना: function signature, समर्थित devices और data types परिभाषित करना
    2. input validation: dimensions, type आदि की जाँच
    3. output tensor allocation
    4. dtype dispatch: data type के अनुसार kernel execution
    5. parallel processing: CPU पर OpenMP, CUDA पर built-in parallelization का उपयोग
    6. data access और computation: TensorAccessor, TensorIterator आदि का उपयोग

प्रमुख dispatch macros

  • AT_DISPATCH_ALL_TYPES → dtype के अनुसार dispatch करता है
  • विभिन्न data types के लिए macro समर्थन → performance optimization संभव

performance optimization और work efficiency बढ़ाने के टिप्स

  • header files में बदलाव कम रखें → बदलाव होने पर पूरा code फिर से rebuild हो सकता है
  • local development environment सेट करें → CI के उपयोग में समय की बर्बादी कम होती है
  • ccache का उपयोग करें → recompilation समय बचाया जा सकता है
  • शक्तिशाली server का उपयोग करें → C++ compilation और CUDA build में समय कम हो सकता है

PyTorch contribution guide

  • शुरुआत के लिए अच्छे contribution targets:
    • triaged label वाले issues → जिन्हें PyTorch developers पहले ही देख चुके हैं
    • documentation सुधार और bug reproduction में मदद
    • PyTorch के RFC (feature proposals) पर राय देना
  • PyTorch open source contributors के माध्यम से बढ़ा है, और community participation का स्वागत है

अभी कोई टिप्पणी नहीं है.

अभी कोई टिप्पणी नहीं है.