Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
@mratsim
mratsim / gru_impl.nim
Created May 10, 2018 21:28
Optimized GRU Cell implementation in Arraymancer
import ../src/arraymancer
import macros
import math
func sigmoid*[T: SomeReal](x: T): T {.inline.} =
1 / (1 + exp(-x))
proc gru_cell_forward[T: SomeReal](input, hidden,
w_input, w_recur,
@mratsim
mratsim / final.nim
Last active May 10, 2018 17:20
Tensor splitting experiment
import ../src/arraymancer
import macros
# dumpASTGen:
# let b = (a[_, 0..<2], a[_, 2..<4], a[_, 4..<6])
macro split[T](x: Tensor[T], n_chunks: static[int], axis = 0): untyped =
## Splits a Tensor into n chunks.
## For efficiency reason, n is required at compile-time
## Split is done without copy, orginal and each chunk share data.

Core tensors operations support

-------------------------------------------------- --------- --------------------------- --------------------------
Feature Tensor CudaTensor ClTensor
-------------------------------------------------- --------- --------------------------- --------------------------
Accessing tensor properties [x] [x] [x] Tensor creation [x] by converting a cpu Tensor by converting a cpu Tensor Accessing or modifying a single value [x] [] [] Iterating on a Tensor [x] [] []
# Works on checkout 8ce9e434348f6f63b81f7a788bd4093996dbaca7, from 11 days ago but not on latest
import opencl
{.experimental.}
type clResource = PCommandQueue | PKernel | PProgram | PMem | PContext
template release*(queue: PCommandQueue) = check releaseCommandQueue(queue)
template release*(kernel: PKernel) = check releaseKernel(kernel)
@mratsim
mratsim / qrdecomp.nim
Created May 3, 2018 11:39
Nim QR decomposition
import ../tensor/tensor, nimlapack
proc syev*(jobz: cstring; uplo: cstring; n: ptr cint; a: ptr cfloat; lda: ptr cint;
w: ptr cfloat; work: ptr cfloat; lwork: ptr cint; info: ptr cint) {.inline.}=
ssyev(jobz, uplo, n, a, lda,
w, work, lwork, info)
proc syev*(jobz: cstring; uplo: cstring; n: ptr cint; a: ptr cdouble; lda: ptr cint;
w: ptr cdouble; work: ptr cdouble; lwork: ptr cint; info: ptr cint) {.inline.}=
packageName = "stint"
version = "0.0.1"
author = "Status Research & Development GmbH"
description = "Efficient stack-based multiprecision int in Nim"
license = "Apache License 2.0 or MIT"
srcDir = "src"
### Dependencies
requires "nim >= 0.18"
@mratsim
mratsim / data_structure.nim
Created May 3, 2018 08:18
Copy-on-Write Tensors
# Copyright 2017 Mamy André-Ratsimbazafy
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@mratsim
mratsim / argmax.nim
Created April 30, 2018 22:50
argmax without openmp for investigation of aliasing
proc streaming_index_max*[T](t: Tensor[T], axis: int): tuple[indices: Tensor[int], maxes: Tensor[T]] =
assert axis in {0, 1}, "Only 1D and 2D tensors are supported at the moment for argmax"
if axis == 0:
result.indices = newTensorUninit[int](1, t.shape[1])
result.maxes = newTensorUninit[T](1, t.shape[1])
else:
result.indices = newTensorUninit[int](t.shape[0], 1)
result.maxes = newTensorUninit[T](t.shape[0], 1)
import macros, tables, hashes
import ../src/arraymancer
proc flatten*(s: openarray[int]): int {.inline.}=
assert s.len != 0
result = 1
import macros
type Conv2DLayer[T] = object
data: T
type Context[T] = ref object
data: T
####################################