Skip to content

Instantly share code, notes, and snippets.

View jackalcooper's full-sized avatar

Shenghang Tsai jackalcooper

View GitHub Profile
@jackalcooper
jackalcooper / latency.markdown
Created October 14, 2016 06:47 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@jackalcooper
jackalcooper / faye_websocket.ex
Last active November 3, 2021 18:33
Elixir Faye Websocket client example, heartbeat and subscription
defmodule Tuna.Client.FayeWebsocket do
import Tuna.Ewhine
require Logger
import Tuna.Ewhine
use Maxwell.Builder, ~w(get post)a
adapter Maxwell.Adapter.Ibrowse
middleware Maxwell.Middleware.BaseUrl, endpoint
middleware Maxwell.Middleware.Headers, %{
"Content-Type" => "application/json",
# "User-Agent" => "Subway",
@jackalcooper
jackalcooper / ONEFLOW_CLA
Last active April 29, 2021 09:50 — forked from CLAassistant/SAP_CLA
SAP Individual Contributor License Agreement
### OneFlow Individual Contributor License Agreement
Thank you for your interest in contributing to open source software projects (“Projects”) made available by OneFlow or its affiliates (“OneFlow”). This Individual Contributor License Agreement (“Agreement”) sets out the terms governing any source code, object code, bug fixes, configuration changes, tools, specifications, documentation, data, materials, feedback, information or other works of authorship that you submit or have submitted, in any form and in any manner, to OneFlow in respect of any of the Projects (collectively “Contributions”). If you have any questions respecting this Agreement, please contact contact@oneflow.org.
You agree that the following terms apply to all of your past, present and future Contributions. Except for the licenses granted in this Agreement, you retain all of your right, title and interest in and to your Contributions.
**Copyright License.** You hereby grant, and agree to grant, to OneFlow a non-exclusive, perpetual, ir
from jax import random, pmap
import jax.numpy as jnp
from jax import grad, jit, vmap
import os
os.environ["XLA_FLAGS"] = "--xla_force_host_platform_device_count=8"
x = jnp.ones((2, 2))
@pmap
defmodule Beaver.Nx.Defn do
require Beaver
import Beaver, only: [mlir: 1]
require Beaver.MLIR.Dialect.Func
alias Beaver.MLIR
alias Beaver.MLIR.Dialect.{Builtin, Func, TOSA}
import Builtin, only: :macros
import MLIR, only: :macros
import MLIR.Sigils
@jackalcooper
jackalcooper / pattern-gen.ex
Last active July 3, 2022 01:25
Question regarding compiling Elixir pattern to MLIR (PDL dialect)
# I’m working on compiling Elixir pattern to MLIR (the PDL dialect),
# so that it could be used to manipulate another piece of IR.
# If we want to rewrite
res = TOSA.add(a, b) >>> Type.ranked_tensor([2, 3], Type.f32())
res1 = TOSA.add(res, b) >>> Type.ranked_tensor([2, 3], Type.f32())
# to
res1 = TOSA.sub(res, b) >>> Type.ranked_tensor([2, 3], Type.f32())
# We can declare a pattern like this
@jackalcooper
jackalcooper / waker.ex
Created July 8, 2022 14:17
Hookup C functions with Enumerable protocol
alias Beaver.MLIR
alias Beaver.MLIR.CAPI.{
MlirModule,
MlirOperation,
MlirRegion,
MlirAttribute,
MlirBlock,
MlirValue,
MlirNamedAttribute,
@jackalcooper
jackalcooper / resource_kind.zig
Created July 27, 2022 01:11
Zig managed NIF resource
pub fn ResourceKind(comptime ElementType: type, module_name: anytype) type {
return struct {
pub const T = ElementType;
pub const module_name = module_name;
pub const resource = struct {
pub var t: resource_type = undefined;
pub const name = @typeName(ElementType);
pub fn make(environment: env, value: T) !term {
return make_resource(environment, value, t);
}
@jackalcooper
jackalcooper / all_close_jit.mlir
Created August 7, 2022 04:56
mlir generated for Nx.all_close
module {
func.func @"Elixir.Manx.Assert.-all_close_jit/3-fun-0-.126722613"(%arg0: tensor<3xf32>, %arg1: tensor<3xf32>) -> tensor<i8> {
%0 = "tosa.const"() {value = dense<9.99999974E-5> : tensor<f32>} : () -> tensor<f32>
%1 = "tosa.const"() {value = dense<0x7F800000> : tensor<3xf32>} : () -> tensor<3xf32>
%2 = "tosa.abs"(%arg0) : (tensor<3xf32>) -> tensor<3xf32>
%3 = "tosa.equal"(%2, %1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xi1>
%4 = "tosa.cast"(%3) : (tensor<3xi1>) -> tensor<3xi8>
%5 = "tosa.abs"(%arg1) : (tensor<3xf32>) -> tensor<3xf32>
%6 = "tosa.equal"(%5, %1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xi1>
%7 = "tosa.cast"(%6) : (tensor<3xi1>) -> tensor<3xi8>
@jackalcooper
jackalcooper / cf_test.exs
Created August 7, 2022 07:06
Some examples
defmodule CfTest do
use ExUnit.Case
use Beaver
alias Beaver.MLIR
alias Beaver.MLIR.Type
alias Beaver.MLIR.Attribute
alias Beaver.MLIR.Dialect.{CF, Arith}
@moduletag :smoke
defmodule MutCompiler do