Skip to content

Instantly share code, notes, and snippets.

@sunrise2575
Created August 4, 2021 06:12
Show Gist options
  • Save sunrise2575/e5dd9c5c10c3deb42087c185842bcdb5 to your computer and use it in GitHub Desktop.
Save sunrise2575/e5dd9c5c10c3deb42087c185842bcdb5 to your computer and use it in GitHub Desktop.
Code snippet collection

μ½”λ“œ μ˜ˆμ‹œ λͺ¨μŒ

@sunrise2575
Copy link
Author

sunrise2575 commented Aug 4, 2021

Python threading (non-blocking, 주의: GILλ•Œλ¬Έμ— 사싀상 single-threadμ—μ„œλ§Œ λ™μž‘)

import threading

def work():
    print("μžμ‹ μ“°λ ˆλ“œ")

t = threading.Thread(target=work)
t.start()
print("λΆ€λͺ¨ μ“°λ ˆλ“œ")
t.join()

GIL(Global Interpreter Lock)

νŒŒμ΄μ¬μ—μ„œλŠ” ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€ μ•ˆμ— λͺ¨λ“  μžμ›μ˜ 락(Lock)을 κΈ€λ‘œλ²Œ(Global)ν•˜κ²Œ κ΄€λ¦¬ν•¨μœΌλ‘œμ¨ ν•œλ²ˆμ— ν•˜λ‚˜μ˜ μ“°λ ˆλ“œλ§Œ μžμ›μ„ μ»¨νŠΈλ‘€ν•˜μ—¬ λ™μž‘ν•˜λ„λ‘ ν•œλ‹€. GIL λ•Œλ¬Έμ— ν•œλ²ˆμ— ν•˜λ‚˜μ˜ μ“°λ ˆλ“œλ§Œ 계산을 μ‹€ν–‰ν•˜μ—¬ μ‹€ν–‰ μ‹œκ°„μ΄ λΉ„μŠ·ν•œ 것이닀. GIL이 μ μš©λ˜λŠ” 것은 cpu λ™μž‘μ—μ„œμ΄κ³  μ“°λ ˆλ“œκ°€ cpu λ™μž‘μ„ 마치고 I/O μž‘μ—…μ„ μ‹€ν–‰ν•˜λŠ” λ™μ•ˆμ—λŠ” λ‹€λ₯Έ μ“°λ ˆλ“œκ°€ cpu λ™μž‘μ„ λ™μ‹œμ— μ‹€ν–‰ν•  수 μžˆλ‹€.

@sunrise2575
Copy link
Author

sunrise2575 commented Aug 4, 2021

Python multiprocessing (non-blocking)

import multiprocessing

def work():
    print("μžμ‹ ν”„λ‘œμ„ΈμŠ€")

t = multiprocessing.Process(target=work)
t.start()
print("λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€")
t.join()

@sunrise2575
Copy link
Author

Javascript coroutine (non-blocking)

async function main(){
  const c = () => new Promise(res => {
    console.log("μžμ‹ 코루틴");
    res();
  });
  console.log("λΆ€λͺ¨ 코루틴");
  await c();

  return 0;
}
main()

@sunrise2575
Copy link
Author

sunrise2575 commented Aug 4, 2021

Rust thread (non-blocking)

use std::thread;

fn main() {
    let t = thread::spawn(move || {
        println!("μžμ‹ μ“°λ ˆλ“œ");
    });
    println!("λΆ€λͺ¨ μ“°λ ˆλ“œ");
    t.join();
}

@sunrise2575
Copy link
Author

Tensorflow single-GPU & multi-GPU matrix multiplication

Python 3.7
TensorFlow 1.13
Numpy 1.16

TensorFlow 1.13버전 μ“°λ €λ©΄ μœ„ 3가지 μ„ΈνŒ… (파이썬, λ„˜νŒŒμ΄ 버전) 이 μ •ν™•νžˆ λ§žμ•„μ•Ό 함

import numpy as np
import datetime
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf


def singleGPU(A, B, n: int):
    print("Single GPU")
    with tf.device('/gpu:0'):
        a = tf.identity(A)
        b = tf.identity(B)
        for _ in range(n):
            a = tf.matmul(a, a)
            b = tf.matmul(b, b)

    with tf.device('/cpu:0'):
        sum = tf.add(a, b)

    t_start = datetime.datetime.now()
    with tf.Session() as sess:
        sess.run(sum)
    t_end = datetime.datetime.now()

    print(t_end - t_start)


def multiGPU(A, B, n: int):
    print("multi GPU")
    with tf.device('/gpu:0'):
        a = tf.identity(A)
        for _ in range(n):
            a = tf.matmul(a, a)

    with tf.device('/gpu:1'):
        b = tf.identity(B)
        for _ in range(n):
            b = tf.matmul(b, b)

    with tf.device('/cpu:0'):
        sum = tf.add(a, b)

    t_start = datetime.datetime.now()
    with tf.Session() as sess:
        sess.run(sum)
    t_end = datetime.datetime.now()

    print(t_end - t_start)


def main():
    with tf.device('/cpu:0'):
        A = tf.random.uniform((1 << 14, 1 << 14), dtype=tf.float32)
        B = tf.random.uniform((1 << 14, 1 << 14), dtype=tf.float32)

    n = 10

    print("A: {}, B: {}".format(A.shape, B.shape))

    singleGPU(A, B, n)
    multiGPU(A, B, n)


main()

@sunrise2575
Copy link
Author

sunrise2575 commented Aug 27, 2021

sequential data 1-d convolution으둜 ν•™μŠ΅ν•˜λŠ” 예제
sequential dataκ°€ scalarκ°€ λ“€μ–΄μ˜€λŠ”κ²Œ μ•„λ‹ˆκ³  vector값이 λ“€μ–΄μ˜€λ©΄ MyModel μ—μ„œ in_channel` 을 vector dimension 만큼 늘렀주면 끝.

μž…λ ₯ 데이터 λ„£κΈ° 전에 -1~1 μ‚¬μ΄λ‘œ 값을 μ •κ·œν™” ν•˜λ©΄ μ„±λŠ₯이 훨씬 ν–₯상될 것.

import torch


class MyModel(torch.nn.Module):
    def __init__(self, time_length, kernel_size):
        super(MyModel, self).__init__()
        self.time_length = time_length
        self.conv = torch.nn.Conv1d(
            in_channels=1, out_channels=32, kernel_size=kernel_size)
        self.fc = torch.nn.Conv1d(
            in_channels=32, out_channels=1, kernel_size=1)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        h = self.relu(self.conv(x[:, :, :self.time_length - 1]))

        y_hat = self.fc(h)
        return y_hat


def main_():
    batch_size = 100
    time_length = 32
    kernel_size = 16
    total_epoch = 350

    x = torch.Tensor([[range(x, x + time_length)] for x in range(batch_size)])
    y = x[:, :, kernel_size:]

    cut = int(0.8 * batch_size)

    x_train, y_train = x[:cut], y[:cut]
    x_test, y_test = x[cut:], y[cut:]

    model = MyModel(time_length=time_length, kernel_size=kernel_size)

    loss_fn = torch.nn.L1Loss()
    optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)

    for epoch in range(350):
        # train
        y_train_hat = model(x_train)
        loss_train = loss_fn(y_train_hat, y_train)

        optimizer.zero_grad()
        loss_train.backward()
        optimizer.step()

        # test
        y_test_hat = model(x_test)
        loss_test = loss_fn(y_test_hat, y_test)

        if epoch % 25 == 0 or total_epoch == 25 - 1:
            print("epoch {:4d}, train loss: {:.3f}, test loss: {:.3f}".format(
                epoch, loss_train.item(), loss_test.item()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment