Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
import macros, tables, hashes
import ../src/tensor/backend/metadataArray
type TopoShape = DynamicStackArray[int]
## TopoShape is compatible but conceptually different from a MetadataArray
## TopoShape does not store the batch size information for tensors
## and is used to describe layers shape
import ../src/arraymancer, random
randomize(42)
let
ctx = newContext Tensor[float32] # Autograd/neural network graph
n = 32 # Batch size
let
x_train = read_mnist_images("build/train-images.idx3-ubyte").astype(float32) / 255'f32
type
TrainableLayer = object {.inheritable.}
## Layer with trainable parameters
weight: int
bias: int
Conv2DLayer{.final.} = object of TrainableLayer
LinearLayer{.final.} = object of TrainableLayer
@mratsim
mratsim / interpreter_bench.nim
Last active April 27, 2018 14:59
Benchmark of interpreter implementation (switch computed goto ...)
import random, sequtils, times
type
Op = enum
Halt # = 0x0000
Inc # = 0x0100
Dec # = 0x0110
Mul2 # = 0x0230
Div2 # = 0x0240
Add7 # = 0x0307
macro myzip(x: ForLoopStmt): untyped =
x.expectKind nnkForStmt
result = newStmtList()
echo x.treeRepr
echo "\n##################"
# Count the number of idents:
var ident_count = 1
while x[ident_count + 1].kind == nnkIdent:
inc ident_count
import macros, ../src/arraymancer
# Transforms
dumpTree:
network model_name:
context: ctx
input_shape: [1, 28, 28] # Real shape [N, 1, 28, 28]
layers:
@mratsim
mratsim / forloop_macro.nim
Created April 21, 2018 16:27
Macro in for-loop
import macros, typetraits
#######################################################
static: echo "########################################"
static: echo "### zip Tree ###"
dumpTree:
iterator zip(a: seq[int], b: seq[bool]): (int,bool) {.noSideEffect.} =
let len = min(a.len, b.len)
# Mpint
# Copyright 2018 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ./uint_type, macros
func `shl`*(x: MpUintImpl, y: SomeInteger): MpUintImpl {.inline.}=
## Compute the `shift left` operation of x and y
# Note: inlining this poses codegen/aliasing issue when doing `x = x shl 1`
# TODO: would it be better to reimplement this using an array of bytes/uint64
# That opens up to endianness issues.
const halfSize = size_mpuintimpl(x) div 2
let defect = halfSize - int(y)
func tohexBE[T: uint8 or uint16 or uint32 or uint64](x: T): string =
let bytes = cast[array[T.sizeof, byte]](x)
result = ""
for i in countdown(T.sizeof - 1, 0):
result.add toHex(bytes[i])
func tohexBE(x: MpUintImpl): string =