Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

A Tour of PyTorch Internals (Part I)

The fundamental unit in PyTorch is the Tensor. This post will serve as an overview for how we implement Tensors in PyTorch, such that the user can interact with it from the Python shell. In particular, we want to answer four main questions:

  1. How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?
  2. How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?
  3. How does PyTorch cwrap work to generate code for Tensor methods?
  4. How does PyTorch's build system take all of these components to compile and generate a workable application?

Extending the Python Interpreter

PyTorch defines a new package torch. In this post we will consider the ._C module. This module is known as an "extension module" - a Python module written in C. Such modules allow us to define new built-in object types (e.g. the Tensor) and to call C/C++ functions.

@sporsh
sporsh / packet-adb.c
Created April 19, 2013 17:26
Android Debug Bridge (ADB) dissector for Wireshark
/*
* packet-adb.c
*
* Routines for Android Debug Bridge (ADB) protocol dissection
* Author: Geir Sporsheim <geir.sporsheim@gmail.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@david-mart
david-mart / pancakeswap-tokenprice.js
Created June 12, 2021 02:00
PancakeSwap Token Price
import Web3 from 'web3';
import { Fetcher, ChainId, Token } from '@pancakeswap/sdk';
import { JsonRpcProvider } from '@ethersproject/providers';
import { BNB_ADDRESS, MAINNET_URL } from '../constants';
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
class Web3Service {
constructor() {
this.web3 = new Web3(MAINNET_URL);
@timyates
timyates / Currying.java
Last active March 7, 2020 07:11
Currying and composition in Java 8
package java8tests ;
import java.util.function.BiFunction ;
import java.util.function.Function ;
public class Currying {
public void currying() {
// Create a function that adds 2 integers
BiFunction<Integer,Integer,Integer> adder = ( a, b ) -> a + b ;
@lolindrath
lolindrath / gist:4454638
Last active January 8, 2020 19:57 — forked from mdb/gist:4288296
How to Amend a Git Commit
# View the log to find the commit you want to edit:
git log
# Quit out of the log
q
# Rebase from the commit you want to edit, in interactive mode:
git rebase SOME_COMMIT_ID^ --interactive
# This will open an interactive menu in Vi
@sporsh
sporsh / parse.py
Created November 22, 2012 00:17
Implementation of Android Debug Bridge (ADB) protocol in Twisted
from adb import protocol
data = (
'\x4f\x50\x45\x4e\x02\x00\x00\x00'
'\x00\x00\x00\x00\x09\x00\x00\x00'
'\x31\x03\x00\x00\xb0\xaf\xba\xb1'
'\x73\x68\x65\x6c\x6c\x3a\x6c\x73'
'\x00'
)
@amir-rahnama
amir-rahnama / dataset.py
Created December 6, 2016 20:39
Write Your Own Custom Image Dataset for Tensorflow
"""A generic module to read data."""
import numpy
import collections
from tensorflow.python.framework import dtypes
class DataSet(object):
"""Dataset class object."""
def __init__(self,
#!/bin/bash
ADBShell () { adb ${2+-s} $2 shell "$1" | tr -d '\r'
}
GetAndroidVersion () {
GOOGLE_SOURCE="https://android.googlesource.com"
REPO="platform/frameworks/base"
ALL_TAGS=$(wget -qO - "$GOOGLE_SOURCE/$REPO/+refs/tags/?format=text" | \
tr -d '^{}' | cut -d/ -f3 | sort -u | grep -vE -- '-(cts|sdk)-' | grep -v "_r0")
@px-amaac
px-amaac / gist:60efd1dba923796d33bb
Last active January 22, 2017 23:43
proguard-rules.txt
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here: