Skip to content

Instantly share code, notes, and snippets.

@simonster
simonster / attention_distance.py
Last active August 31, 2024 19:43
Mean attention distance
# Copyright 2022 Google LLC.
# SPDX-License-Identifier: Apache-2.0
# Author: Maithra Raghu <[email protected]>
def compute_distance_matrix(patch_size, num_patches, length):
"""Helper function to compute distance matrix."""
distance_matrix = np.zeros((num_patches, num_patches))
.-"-._( ) )
/ , , 66\
`======\ )_\\=\_o)=
`~~"~ `"
using Compat
# Original C version at https://naml.us/post/inverse-of-a-hash-function/
function inverse_hash(key::UInt64)
local tmp::UInt64
# Invert key = key + (key << 31)
tmp = key-(key<<31)
key = key-(tmp<<31)

To the members of the MIT community:

We are writing to inform you of plans to upgrade the MIT campus network, and in particular to upgrade MIT to the next generation of Internet addressing. (Please note that no action is required on your part.)

Machines on the Internet are identified by addresses. The current addressing scheme, called IPv4, was specified around 1980, and allowed for about 4 billion addresses. That seemed enough at the time, which was before local area networks, personal computers and the like, but the Internet research community recognized around 1990 that this supply of addresses was inadequate, and put in place a plan to replace the IPv4 addresses with a new address format, called IPv6. IPv6 uses a 128-bit address scheme and is capable of 340 undecillion addresses (340 times 10^36, or 340 trillion trillion trillion possible IP addresses). This stock of addresses allows great flexibility in how addresses are assigned to hosts, for example allowing every host to use a range of addresses to

if (platform == "win") {
libc = ctypes.open("Ws2_32.dll");
var dnsapi = ctypes.open("Dnsapi.dll");
var DNS_RECORD = new ctypes.StructType("DNS_RECORD");
DNS_RECORD.define([{"pNext":DNS_RECORD.ptr}, {"pName":ctypes.char.ptr}, {"wType":ctypes.unsigned_short},
{"wDataLength":ctypes.unsigned_short}, {"DW":ctypes.unsigned_long}, {"dwTtl":ctypes.unsigned_long},
{"dwReserved":ctypes.unsigned_long}, {"pNameHost":ctypes.char.ptr}]);
var DnsQuery = dnsapi.declare("DnsQuery_A", ctypes.winapi_abi, ctypes.int, ctypes.char.ptr, ctypes.unsigned_short,
ctypes.unsigned_long, ctypes.voidptr_t, DNS_RECORD.ptr, ctypes.voidptr_t);
@simonster
simonster / gist:b1b4cc2ad0daa8e20a99
Last active August 29, 2015 14:11
missingness sum benchmark
immutable CheapMulBool
x::Bool
end
immutable Negated{T}
x::T
end
immutable BitWrapper
x::Vector{Uint64}
end
Base.getindex(x::BitWrapper, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x, i))
@simonster
simonster / alist.jl
Last active August 29, 2015 14:07
alist
immutable AListWrapper{Name}; end
immutable AList{Keys<:(AListWrapper...),Types} <: Associative
v::Types
end
macro alist(args...)
keys = Array(Any, length(args))
values = Array(Any, length(args))
for i = 1:length(args)
if !Base.Meta.isexpr(args[i], :(=>))
@simonster
simonster / divinverse.jl
Last active August 29, 2015 14:06
Faster vectorized integer division
import Base.rem, Base.div, Base.divrem
unsigned_type(::Int8) = Uint8
unsigned_type(::Int16) = Uint16
unsigned_type(::Int32) = Uint32
unsigned_type(::Int64) = Uint64
unsigned_type(::Int128) = Uint128
immutable SignedMultiplicativeInverse{T<:Signed}
divisor::T
multiplier::T
@simonster
simonster / gist:6195af68c6df33ca965d
Last active August 29, 2015 14:05
Magic square
function iain_magic(n::Int)
# Odd-order magic square
# http://en.wikipedia.org/wiki/Magic_square#Method_for_constructing_a_magic_square_of_odd_order
M = zeros(Int, n, n)
for I = 1:n, J = 1:n # row, column
@inbounds M[I,J] = n*((I+J-1+(n >> 1))%n) + ((I+2J-2)%n) + 1
end
return M
end
@simonster
simonster / h5dump test.jld
Last active August 29, 2015 14:05
jld immutables
HDF5 "test.jld" {
GROUP "/" {
DATASET "ComplexImmutable" {
DATATYPE "/_types/3"
DATASPACE SCALAR
DATA {
(0): {
{
1,
2.2